rspec测试问题,未定义的方法"post" [英] problem with rspec test, undefined method 'post'

查看:75
本文介绍了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非常陌生.任何帮助,将不胜感激.谢谢.

解决方案

如果spec文件不在spec/controllers下,则rspec-rails不会自动提供getpost之类的方法. >

您需要标记您的规格:

describe MyController, type: :controller do
  # ...
end

或包含模块:

describe MyController do
  include RSpec::Rails::ControllerExampleGroup
  # ...
end

请参见 rspec-rails中的相关代码.

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.

解决方案

If the spec file is not under spec/controllers, methods like get and post will not be automatically made available by rspec-rails.

You either need to tag your spec:

describe MyController, type: :controller do
  # ...
end

or include the module:

describe MyController do
  include RSpec::Rails::ControllerExampleGroup
  # ...
end

See the relevant code in rspec-rails.

这篇关于rspec测试问题,未定义的方法"post"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆