不确定这是Rails 3问题还是RSpec 2问题,但我似乎无法让标准控制器测试工作 - 似乎无法找到'get'方法。
我有一个看起来像这样的控制器测试(在discrepancies_controller_spec.rb
目录中名为spec/controllers
):
require 'spec_helper'
describe DiscrepanciesController do
before :each do
Discrepancy.delete_all
end
it "resolves a discrepancy" do
discrepancy = Discrepancy.create(:my_number=>"12345", :status=>"Open")
get :resolve, :id => discrepancy.id
retrieved_discrepancy = Discrepancy.find_by_my_number("12345")
retrieved_discrepancy.status.should == "Resolved"
end
end
(是的,我知道使用HTTP / GET修改数据的安全隐患 - 这是一个单独的问题......)
当我用rake运行它时,我收到以下错误:
1) DiscrepanciesController resolves a discrepanc
Failure/Error: Unable to find C to read failed line
undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0xc9170d0 @__memoized={}>
# ./spec/controllers/discrepancies_controller_spec.rb:38 (ignore the line number, commented out code was removed from the sample)
# C:/Users/Patrick_Gannon/.bundle/ruby/1.8/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:212:in `inject'
# C:/Ruby187/bin/rake:19:in `load'
# C:/Ruby187/bin/rake:19
我可以通过自己实例化控制器并直接调用控制器操作来测试控制器操作,但它很有效,但它很难看,因为我必须嘲笑respond_to
和params
之类的东西。
其他相关信息:我运行Windows 7 32位,Ruby 1.8.7(2010-01-10 patchlevel 249)[i386-wingw32],边缘Rails 3和MongoDB / MongoMapper。这是我安装的宝石列表(通过'bundle gem' - 所有我安装的宝石都是由Bundler安装的)
我还在测试中添加了一个puts语句,以显示测试夹具中可用的方法((methods - Object.methods).sort.inspect
),并且“get”不在列表中。这是列表中的内容:
"__memoized", "__should_for_example_group__", "__should_not_for_example_group__", "_fixture_class_names", "_fixture_path", "_fixture_table_names", "_pre_loaded_fixtures", "_setup_mocks", "_teardown_mocks", "_use_instantiated_fixtures", "_use_transactional_fixtures", "_verify_mocks", "a_kind_of", "allow_message_expectations_on_nil", "an_instance_of", "any_args", "anything", "assert", "assert_block", "assert_equal", "assert_in_delta", "assert_instance_of", "assert_kind_of", "assert_match", "assert_nil", "assert_no_match", "assert_not_equal", "assert_not_nil",
"assert_not_same", "assert_nothing_raised", "assert_nothing_thrown", "assert_operator", "assert_raise", "assert_raises", "assert_respond_to", "assert_same", "assert_send", "assert_throws", "be", "be_a", "be_a_kind_of", "be_a_new", "be_an", "be_an_instance_of", "be_close", "be_false", "be_instance_of", "be_kind_of", "be_nil", "be_true", "boolean", "build_message", "change", "described_class", "double", "duck_type", "eq", "eql", "equal", "example", "example=", "exist", "expect", "fixture_class_names", "fixture_class_names?", "fixture_path", "fixture_path?", "fixture_table_names",
"fixture_table_names?", "flunk", "hash_including", "hash_not_including", "have", "have_at_least", "have_at_most", "have_exactly", "include", "instance_of", "kind_of", "match", "method_missing", "method_name", "mock", "mock_discrepancy", "mock_model", "no_args", "pending", "pre_loaded_fixtures", "pre_loaded_fixtures?", "raise_error", "respond_to", "run_in_transaction?", "running_example", "satisfy", "setup_fixtures", "stub_model", "subject", "teardown_fixtures", "throw_symbol", "use_instantiated_fixtures", "use_instantiated_fixtures?", "use_transactional_fixtures", "use_transactional_fixtures?"
答案 0 :(得分:4)
在David Chelimsky的RSpec邮件列表上找到了答案如下:
我很惊讶这是第一次提出rspec-2,但我们在这里:)
这是一个路径分隔符错误,我将在下一个版本中解决。目前,您可以在控制器规范中执行此操作:
describe DiscrepanciesController do
include RSpec::Rails::ControllerExampleGroup
这应该可以正常工作。
如果您想要进行更全面的解决方法,请将其添加到spec_helper配置:
RSpec.configure do |c|
c.include RSpec::Rails::ControllerExampleGroup, :example_group => { :file_path => /\bspec[\\\/]controllers[\\\/]/ }
end