rspec 测试出现问题,未定义方法“post”;
我正在编写一个规范来测试当有人通过 URL 发送查询时 mashup_controller 的行为。我需要模拟 URL 中包含的参数,并且我读到 post() 方法会执行此操作,但是当我收到错误时:
1) MashupController simulates query
Failure/Error: post :create
NoMethodError:
undefined method `post' for
#<RSpec::Core::ExampleGroup::Nested_1:0x980bc50>
# ./mashup_controller_rspec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.20199 seconds 1 example, 1 failure
Failed examples:
rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query
这是我的代码:
require 'spec_helper'
require 'mashup_controller.rb'
describe MashupController do
it "simulates query" do
post :create
end
end
抱歉,如果我没有任何意义。我对 Rails 和 rspec 非常陌生。任何帮助将不胜感激。谢谢。
I am writing a spec to test the behavior of the mashup_controller when someone sends a query through a URL. I need to simulate the parameters contained in the URL, and i read that the post() method will do that, however when i get an error:
1) MashupController simulates query
Failure/Error: post :create
NoMethodError:
undefined method `post' for
#<RSpec::Core::ExampleGroup::Nested_1:0x980bc50>
# ./mashup_controller_rspec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.20199 seconds 1 example, 1 failure
Failed examples:
rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query
Here is my code:
require 'spec_helper'
require 'mashup_controller.rb'
describe MashupController do
it "simulates query" do
post :create
end
end
Sorry if I'm not making any sense. I am very new to rails and rspec. Any help would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果规范文件不在
spec/controllers
下,则rspec-rails 将不会自动提供
。get
和post
等方法您需要标记您的规范:
或包含模块:
请参阅 rspec-rails中的相关代码。
If the spec file is not under
spec/controllers
, methods likeget
andpost
will not be automatically made available byrspec-rails
.You either need to tag your spec:
or include the module:
See the relevant code in rspec-rails.
gem spec-rails
mashup_controller_rspec.rb
应位于spec/controllers
下gem spec-rails
in your Gemfilemashup_controller_rspec.rb
should be underspec/controllers
我使用 gem rspec-rails 而不是 gem spec-rails。
I used gem rspec-rails instead of gem spec-rails.
在Rails 4中,您可以将RSpec测试的类型声明为
:request
,并且spec文件可以位于任何目录中。In Rails 4, you can declare the type of the RSpec tests as
:request
and the spec file can be in any directory.我的解决方案是
My solution is