在RSPEC上遇到ActionController :: UrlGenerationError问题

时间:2017-01-17 12:33:41

标签: ruby-on-rails ruby rspec

我正在为我的控制器编写一些测试,但是当我运行rspec ./spec/controllers时我得到了以下错误,当我直接在文件中运行规范时,我将所有内容都变为绿色。

1) VersionOne::UsersController GET #actives should return all actives
 Failure/Error: get :actives

 ActionController::UrlGenerationError:
   No route matches {:action=>"actives", :controller=>"version_one/users"}
 # ./spec/controllers/version_one/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

2) VersionOne::UsersController GET #archived should return all archives
 Failure/Error: get :archives

 ActionController::UrlGenerationError:
   No route matches {:action=>"archives", :controller=>"version_one/users"}
 # ./spec/controllers/version_one/users_controller_spec.rb:23:in `block (3 levels) in <top (required)>'

3) VersionOne::UsersController POST #create should create a new collaborator with success
 Failure/Error: post :create, params: params

这是我的控制器

class UsersController < ApiControler
  def actives
    users = User.actives
    render json: users
  end

  def archives
    users = User.archiveds
    render json: users
 end

  def create
    user = User.build(user_params)
    render json: user
  end
end

这是我的路线

  scope 'api' do
    scope 'v1', module: 'version_one' do
      resources 'users' do
        collection do
          get 'actives'
          get 'archives'
        end

        member do
          match 'active'
          match 'archive'
        end
      end
    end
  end

这是我的测试

RSpec.describe VersionOne::UsersController, type: :controller do
  before(:all) do
    7.times { Collaborator.build(attributes_for(:user)) }
    8.times { Admin.build(attributes_for(:user)) }
    5.times { Collaborator.build(attributes_for(:user)).archived! }
  end

  describe "GET #actives" do
    it "should return all actives" do
    get :actives
    body = JSON.parse(response.body)
    expect(response).to have_http_status(:success)
    expect(body["pagination"]["per_page"]).to eq(20)
    expect(body["pagination"]["total_objects"]).to eq(15)
  end
 end

 describe "GET #archived" do
   it "should return all archives" do
     get :archives
     body = JSON.parse(response.body)
     expect(response).to have_http_status(:success)
     expect(body["pagination"]["per_page"]).to eq(20)
     expect(body["pagination"]["total_objects"]).to eq(5)
   end
 end

 describe "POST #create" do
   it "should create a new collaborator with success" do
     params = { kind: "Collaborator", user: { name: "User", email: "user@email.net", password: "123456", cpf: "36156291830" } }
     post :create, params: params
     body = JSON.parse(response.body)
     expect(response).to have_http_status(:success)
     expect(body).to include(
       "name" => "User",
       "cpf" => "36156291830",
       "email" => "user@email.net",
      )
    end
  end
end

这是我的.rspec

--color
--format documentation
--require rails_helper

这是我的rails_helper

ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'spec_helper'
require 'rspec/rails'
require 'shoulda-matchers'
require 'rack/test'
require 'faker'
require 'rake'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.include Shoulda::Matchers::ActiveModel, type: :model
  config.include Shoulda::Matchers::ActiveRecord, type: :model
  config.fixture_path = "#{::Rails.root}/spec/factories"
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end

  config.use_transactional_fixtures = false
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!

end
我忘记了什么?

1 个答案:

答案 0 :(得分:1)

您没有UsersController的命名空间 尝试:

module Api
  module VersionOne
    class UsersController < ApiController
        # your code
    end
  end
end

您可能还需要将ApiController更改为Api::VersionOne::ApiController