Using helpers in minitest

为君一笑 提交于 2020-01-24 01:11:08

问题


I'm trying to test my app class in using minitest but I am getting a error stating undefined method image_path. How can I get around this?

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

even when I do something like

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

I get the same error because of the app model


回答1:


Try:

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

Ref: https://stackoverflow.com/a/7465398/429758



来源:https://stackoverflow.com/questions/29437053/using-helpers-in-minitest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!