在 minitest 中使用助手
Using helpers in minitest
我正在尝试使用 minitest 测试我的应用程序 class,但我收到一条错误消息 undefined method image_path
。我该如何解决这个问题?
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/models/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
由于应用程序模型,我得到了同样的错误
尝试:
assert_equal @app.icon_url, ApplicationController.helpers.image_path('icn-medium-norm.png')
参考:
我正在尝试使用 minitest 测试我的应用程序 class,但我收到一条错误消息 undefined method image_path
。我该如何解决这个问题?
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/models/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
由于应用程序模型,我得到了同样的错误
尝试:
assert_equal @app.icon_url, ApplicationController.helpers.image_path('icn-medium-norm.png')
参考: