【发布时间】: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