rspec 测试出现问题,未定义方法“post”;

发布于 2024-11-30 09:17:32 字数 785 浏览 3 评论 0原文

我正在编写一个规范来测试当有人通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

聊慰 2024-12-07 09:17:32

如果规范文件不在 spec/controllers 下,则 rspec-rails 将不会自动提供 getpost 等方法

您需要标记您的规范:

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

或包含模块:

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

请参阅 rspec-rails中的相关代码

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.

·深蓝 2024-12-07 09:17:32
  1. 确保您的 Gemfile 中有 gem spec-rails
  2. 您的 mashup_controller_rspec.rb 应位于 spec/controllers
  1. Make sure you have gem spec-rails in your Gemfile
  2. Your mashup_controller_rspec.rb should be under spec/controllers
鹤舞 2024-12-07 09:17:32

我使用 gem rspec-rails 而不是 gem spec-rails。

I used gem rspec-rails instead of gem spec-rails.

浅语花开 2024-12-07 09:17:32

在Rails 4中,您可以将RSpec测试的类型声明为:request,并且spec文件可以位于任何目录中。

example: in spec/routes/users.rb
RSpec.describe 'UserRoutes', type: :request do
  ...
end

In Rails 4, you can declare the type of the RSpec tests as :request and the spec file can be in any directory.

example: in spec/routes/users.rb
RSpec.describe 'UserRoutes', type: :request do
  ...
end
别念他 2024-12-07 09:17:32

我的解决方案是

describe MyController, type: :controller
...
end

My solution is

describe MyController, type: :controller
...
end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文