Rspec控制器工作,规格失败

时间:2014-07-09 15:18:52

标签: ruby-on-rails rspec

我是一个简单的控制器,似乎正常运行,但规格失败了。

以下是规范:

require 'rails_helper'

RSpec.describe UsersController, :type => :controller do

  include Devise::TestHelpers

  let(:user) { FactoryGirl.create(:user) }
  let(:user1) { FactoryGirl.create(:user) }

  describe "#index" do  

    before {sign_in user}

    it "assigns a varaible @users" do 
      get :index
      expect(assigns(:users)).to be
    end

    it "includes all users in database" do 
      user = FactoryGirl.create(:user)
      get :index
      expect(assigns(:users)).to include(user)
    end

    it "if approved==false only user awaiting approval are shown" do 
      get :index, { approved: false}
      expect(assigns(:users)).to be
    end

    it "should only fetches 10 users" do
      11.times { FactoryGirl.create(:user) }
      get :index
      expect(assigns(:users).length).to eq(10)
    end

  end

end

以下是用户#index:

  def index
     if params[:approved] == "false"
       @users = User.where(approved: false).page(params[:page]).per(10)
     else
       @users = User.all.page(params[:page]).per(10)
     end
   end

以下是错误报告:

Failures:

  1) UsersController#index assigns a varaible @users
     Failure/Error: expect(assigns(:users)).to be
       expected nil to evaluate to true
     # ./spec/controllers/users_controller_spec.rb:17:in `block (3 levels) in <top (required)>'

  2) UsersController#index includes all users in database
     Failure/Error: expect(assigns(:users)).to include(user)
     NoMethodError:
       undefined method `include?' for nil:NilClass
     # ./spec/controllers/users_controller_spec.rb:24:in `block (3 levels) in <top (required)>'

  3) UsersController#index if approved==false only user awaiting approval are shown
     Failure/Error: expect(assigns(:users)).to be
       expected nil to evaluate to true
     # ./spec/controllers/users_controller_spec.rb:29:in `block (3 levels) in <top (required)>'

  4) UsersController#index should only fetches 10 users
     Failure/Error: expect(assigns(:users).length).to eq(10)
     NoMethodError:
       undefined method `length' for nil:NilClass
     # ./spec/controllers/users_controller_spec.rb:35:in `block (3 levels) in <top (required)>'

  5) UsersController#new with signed in user assigns a new user
     Failure/Error: expect(assigns(:user)).to be_a_new(User)
       expected nil to be a new User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, confirmation_token: string, confirmed_at: datetime, confirmation_sent_at: datetime, is_admin: boolean, approved: boolean)
     # ./spec/controllers/users_controller_spec.rb:57:in `block (4 levels) in <top (required)>'

  6) UsersController#new with signed in user renders the new view
     Failure/Error: expect(response).to render_template(:new)
       expecting <"new"> but rendering with <[]>
     # ./spec/controllers/users_controller_spec.rb:62:in `block (4 levels) in <top (required)>'

Finished in 0.16484 seconds (files took 2.95 seconds to load)
8 examples, 6 failures

Failed examples:

rspec ./spec/controllers/users_controller_spec.rb:15 # UsersController#index assigns a varaible @users
rspec ./spec/controllers/users_controller_spec.rb:21 # UsersController#index includes all users in database
rspec ./spec/controllers/users_controller_spec.rb:27 # UsersController#index if approved==false only user awaiting approval are shown
rspec ./spec/controllers/users_controller_spec.rb:32 # UsersController#index should only fetches 10 users
rspec ./spec/controllers/users_controller_spec.rb:55 # UsersController#new with signed in user assigns a new user
rspec ./spec/controllers/users_controller_spec.rb:60 # UsersController#new with signed in user renders the new view

我不知道问题是什么。任何帮助将不胜感激。

0 个答案:

没有答案