problem with rspec test, undefined method 'post'

让人想犯罪 __ 提交于 2019-11-30 17:07:03

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.

  1. Make sure you have gem spec-rails in your Gemfile
  2. Your mashup_controller_rspec.rb should be under spec/controllers

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

RoundOutTooSoon

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

My solution is

describe MyController, type: :controller
...
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!