2015-04-03 43 views
0

我正在嘗試使用minitest來測試我的應用程序類,但我收到一條錯誤消息,指出undefined method image_path。我怎樣才能解決這個問題?在minitest中使用幫助程序

app_test.rb

require 'test_helper' 

class AppTest < ActiveSupport::TestCase 

    setup do 
    @app = apps(:app_one) 
    end 

    test 'should have icon_url' do 
    assert(@app.icon_url == image_path('icn-medium-norm.png')) 
    end 
end 

應用程序/模型/ app.rb

class App < ActiveRecord::Base 
    has_many :versions, dependent: :destroy 
    has_many :user_subscriptions, dependent: :destroy 
    has_many :users, through: :user_subscriptions 
    validates :name, uniqueness: { case_sensitive: false, scope: :app_type } 

    scope :since, ->(time) { where('created_at > ?', time) } 
    scope :ios, -> { where("app_type = 'ios' ") } 
    scope :android, -> { where("app_type = 'android' ") } 

    def icon_url 
    versions.last[:icon_url] || image_path('icn-medium-norm.png') 
    end 
    ... 
end 

甚至當我這樣做

test 'should have icon_url' do 
    assert(@app.icon_url =~ %r{.png}) 
end 

我得到的,因爲應用程序的同樣的錯誤模型

+0

看看[這裏](http://guides.rubyonrails.org/testing.html#integration-testing),如果有幫助.. – 2015-04-03 17:52:51

回答

1

嘗試:

assert_equal @app.icon_url, ApplicationController.helpers.image_path('icn-medium-norm.png') 

編號:https://stackoverflow.com/a/7465398/429758

+0

還是同樣的錯誤。 – 2015-04-03 17:59:53

+0

該錯誤將來自'versions.last [:icon_url] || image_path('icn-medium-norm.png')'行。視圖助手在模型中不可見;你也必須在那裏做同樣的改變。 – 2015-04-03 18:01:13