错误的rspec错误显示

时间:2014-05-15 11:03:35

标签: ruby-on-rails ruby-on-rails-4 rspec controller

控制器:

class ProjectsController < ApplicationController
  before_filter :authenticate_user!
  load_and_authorize_resource except: :create
  skip_authorize_resource only: [:update, :destroy]
  rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
  respond_to :json

  def create
    authorize! :create, Project
    project = Project.new(project_params)
    if project.save
      current_user.add_role(:manager, project)
      project.colors.create(user_id: current_user.id)
    end
    respond_with project, serializer: ProjectSerializer
  end
...

规格:

require 'spec_helper'
describe ProjectsController do
  include Devise::TestHelpers
  include UserHelpers

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

  before(:each) do
    # controller.stub(:authenticate_user!).and_return true
    sign_in user
    ability_setup
  end

  context "create" do
    it "Can not create project" do
      @ability.cannot :create, Project
      project = {name: 'Test project'}
      post :create, format: 'json', project: project
      expect(response).to be_success
    end
  end
...

如果我将测试中的最后一行更改为expect(response).to_not be_success,那么一切正常!但是,如果它像现在一样,我得到了

Failure/Error: post :create, format: 'json', project: project
     NoMethodError:
       undefined method `authenticate!' for nil:NilClass

为什么这么奇怪的错误?如果我评论before_filter:authenticate_user!在控制器中,它变得正常 - expected success? to return true, got false

期望(响应)如何影响身份验证! (看起来像监狱长的方法?)

1 个答案:

答案 0 :(得分:0)

看起来问题出在RSpec :: Retry gem中,行:

config.verbose_retry = true

config.default_retry_count = 3 

导致RSpec与设备显示错误错误