rspec希望您的控制器规格位于/ spec / controllers中。我有一些模块包含在我的控制器中。正如我所知,测试它们的唯一方法是创建一个虚拟控制器,在其中包含模块,然后从那里进行测试。问题是模块规格在/ spec / lib / module中,因此不包含ControllerExampleGroup,这意味着我无法调用辅助方法得到“动作”等。
我可以手动导入这些,这让我大部分都在那里,但我得到了这个错误:
Failure/Error: Unable to find matching
line from backtrace undefined method
`path_set' for nil:NilClass
activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in
`method_missing'
rspec-rails-2.2.1/lib/rspec/rails/view_rendering.rb:68
rspec-core-2.2.1/lib/rspec/core/example_group.rb:291:in
`instance_eval'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:291:in
`instance_eval_with_rescue'
rspec-core-2.2.1/lib/rspec/core/hooks.rb:39:in
`run_in'
rspec-core-2.2.1/lib/rspec/core/hooks.rb:70:in
`run_all'
rspec-core-2.2.1/lib/rspec/core/hooks.rb:70:in
`each'
rspec-core-2.2.1/lib/rspec/core/hooks.rb:70:in
`run_all'
rspec-core-2.2.1/lib/rspec/core/hooks.rb:106:in
`run_hook'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:194:in
`eval_after_eachs'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:194:in
`each'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:194:in
`eval_after_eachs'
rspec-core-2.2.1/lib/rspec/core/example.rb:130:in `run_after_each'
rspec-core-2.2.1/lib/rspec/core/example.rb:44:in `run'
rspec-core-2.2.1/lib/rspec/core/example.rb:88:in `with_around_hooks'
rspec-core-2.2.1/lib/rspec/core/example.rb:37:in `run'
rspec-core-2.2.1/lib/rspec/core/example.rb:81:in `with_pending_capture'
rspec-core-2.2.1/lib/rspec/core/example.rb:80:in `catch'
rspec-core-2.2.1/lib/rspec/core/example.rb:80:in `with_pending_capture'
rspec-core-2.2.1/lib/rspec/core/example.rb:36:in `run'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:261:in
`run_examples'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:257:in
`map'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:257:in
`run_examples'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:231:in
`run'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:232:in
`run'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:232:in
`map'
rspec-core-2.2.1/lib/rspec/core/example_group.rb:232:in
`run'
rspec-core-2.2.1/lib/rspec/core/command_line.rb:27:in
`run'
rspec-core-2.2.1/lib/rspec/core/command_line.rb:27:in
`map'
rspec-core-2.2.1/lib/rspec/core/command_line.rb:27:in
`run'
rspec-core-2.2.1/lib/rspec/core/reporter.rb:12:in `report'
rspec-core-2.2.1/lib/rspec/core/command_line.rb:24:in
`run'
rspec-core-2.2.1/lib/rspec/core/runner.rb:55:in
`run_in_process'
rspec-core-2.2.1/lib/rspec/core/runner.rb:46:in
`run'
rspec-core-2.2.1/lib/rspec/core/runner.rb:10:in
`autorun'
/Users/jeffdeville/.rvm/gems/ree-1.8.7-2010.02/bin/rspec:19
建议?
答案 0 :(得分:8)
如果您的规范不在其中一个标准位置,您可以通过将:type属性传递给describe方法来告诉rspec:
describe MyController, :type => :controller do
...
end
对于要包含在控制器中的模块,rspec 2有一种非常方便的方式来创建匿名控制器。
describe MyModule do
context "when included by a controller", :type => :controller do
controller do # anonymous subclass of ActionController::Base
include MyModule
def index
head :ok
end
end
it "works" do
get "index"
response.should be_success
end
end
end
rspec docs中的更多信息(由Cucumber功能生成):http://relishapp.com/rspec/rspec-rails/dir/controller-specs/anonymous-controller