【问题标题】:RSpec NoMethodError: undefined method `assign'RSpec NoMethodError:未定义的方法“分配”
【发布时间】:2014-08-01 01:51:08
【问题描述】:

我正在尝试为我为 Rails 编写的一个小 gem 编写测试。我想独立于虚拟应用程序测试 gem,觉得这对我来说是一次很好的学习体验。但是,由于上述错误,我一直无法通过简单的测试。

我的 Gemfile 指向 gemspec,我用 rspec --init 初始化了 RSpec。

我的测试(只是为了确保在我开始 ernest 之前一切都设置正确):

RSpec.describe 'text_field', type: :view do
    it 'has something on the page' do
        assign(:users, [
          mock_model("User", id: 1, name: 'Ashley')
        ])
        stub_template 'users/_form.html.erb' => "<%= form_for @user do |f| %><%= f.text_field :name' %><% end %>"
        render
        rendered.should =~ /Ashley/
    end
end

整个错误:

text_field
  has something on the page (FAILED - 1)

Failures:

  1) text_field has something on the page
     Failure/Error: assign(:users, [
     NoMethodError:
       undefined method `assign' for #<RSpec::ExampleGroups::TextField:0x007faa143e8828>
     # ./spec/tests/text_field_spec.rb:4:in `block (2 levels) in <top (required)>'

Finished in 0.03812 seconds (files took 0.22166 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/tests/text_field_spec.rb:3 # text_field has something on the page

Top 1 slowest examples (0.03727 seconds, 97.8% of total time):
  text_field has something on the page
    0.03727 seconds ./spec/tests/text_field_spec.rb:3

我的宝石规格:

  ...
  spec.test_files    = Dir["spec/**/*.rb"]

  spec.required_ruby_version = "2.1.2"
  spec.add_development_dependency "bundler", "1.6.3"
  spec.add_development_dependency "rails", "4.1.4"
  spec.add_development_dependency "rspec-rails", "3.0.1"
  spec.add_development_dependency "rspec-activemodel-mocks", "1.0.1"
  ...

我的 spec_helper.rb:

require 'rspec/active_model/mocks'

RSpec.configure do |config|

  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  if config.files_to_run.one?
    config.default_formatter = 'doc'
  end

  config.profile_examples = 10
  config.order = :random

  Kernel.srand config.seed

  config.expect_with :rspec do |expectations|
    expectations.syntax = :expect
  end

  config.mock_with :rspec do |mocks|
    mocks.syntax = :expect
    mocks.verify_partial_doubles = true
  end

end

我已将测试类型指定为视图,因为我对 RSpec 文档的理解是 assigns 仅在视图中可用。我是否错过了某个重要的步骤,或者我错过了什么?

【问题讨论】:

  • 您找到解决方案了吗?我在升级到 rspec(-rails) 3 时遇到了这个问题
  • 不,还没有。一旦我更好地掌握了 RSpec 和 Rails 内部,它仍然在我的完成列表中。
  • 我显然错过了, type: :view 部分,这在 rspec3 中是新的,现在我的正在工作......
  • 不确定,但是看看这个链接,它可能会有所帮助[这个链接][1] [1]:stackoverflow.com/questions/5492578/…

标签: ruby-on-rails gem rspec-rails nomethoderror


【解决方案1】:

正如 Jarl 所说,答案是将type: view 放在您的测试文件中的某个位置。

require 'spec_helper'

describe 'search/print/_asset.html.haml', type: :view do
  it 'displays the page' do
    asset = build(:asset)
    render 'search/print/asset', asset: asset
    assert_select ":match('href', ?)", "/asset/#{asset.property_id}"
  end
end

您可以更有选择性地放置它。这也有效:it 'displays the page', type: view do ... end

【讨论】:

    【解决方案2】:

    你有一个错字。你使用了assign,但应该使用assigns

    【讨论】:

    • 我想知道是不是这样,我已经尝试过了。但是,从docs 开始,它是assign(key, val),带有升级说明# rspec-rails-1.x assigns[key] = value # rspec-rails-2.x+ assign(key, value)
    • 不,assigns 用于测试控制器规范中的预期。 assign 用于在视图规范中设置分配。
    猜你喜欢
    • 2014-09-26
    • 2017-12-09
    • 2016-05-25
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2018-04-20
    相关资源
    最近更新 更多