ruby-on-rails - rspec 测试有问题,未定义的方法 'post'

标签 ruby-on-rails ruby rspec tdd bdd

我正在编写规范来测试当有人通过 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 下, 将不会自动提供 getpost 等方法rspec-rails.

您要么需要标记您的规范:

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

或包含模块:

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

参见 the relevant code in rspec-rails .

关于ruby-on-rails - rspec 测试有问题,未定义的方法 'post',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7135377/

相关文章:

ruby-on-rails - 带 Letsencrypt 的 Heroku - 无法使用插件

ruby-on-rails - 如何在 Rails 中禁用数据后重新启用提交按钮

ruby-on-rails - 如何分析我的 rspec 测试以找到最大的内存消耗?

ruby - 检查集合以包含满足 lambda 的项的 RSpec 匹配器

ruby-on-rails - 了解 routes.rb 中的 "do, end"语法

ruby-on-rails - Mongoid 3.0 嵌入 1-N 查询,用于列出所有嵌入字段

ruby-on-rails - 帮助程序中的 Rails Form block - 我如何包含 "Protect from forgery"

c - Ruby/C/Makefile,-lpcap/#include <pcap.h> 中使用的默认 pcap.h 文件是什么

ruby - 正则表达式获取两个管道之间的内容并返回一个空格,其中两个管道彼此相邻且没有空格

rspec - 如何在渲染 View 上测试文本?