Cancan的'load_and_authorize_resource'调用Model.new导致RSpec测试中出现意外故障

时间:2011-12-07 20:04:10

标签: ruby-on-rails rspec cancan

使用RSpec和Cancan,我的测试失败了:

Failure/Error: User.should_receive(:new).and_return(@user)
expected: 1 time
received: 2 times

,因为 load_and_authorize_resource似乎也在对象上调用'new'。我该如何解决这个问题?

it "creates a new staff member" do
  User.should_receive(:new).and_return(@user)
  get :new, :format => "js"
end

-

 class Admin::UsersController < ApplicationController

    load_and_authorize_resource

    def new
      @user = User.new()
      respond_to do |format|
        format.js { render :action => "new" }
      end
    end
  end

1 个答案:

答案 0 :(得分:1)

好:

  • load_and_authorize_resource替换为authorize_resource

或者:

  • 删除@user = User.new()(带有不必要的括号)

你肯定在这里做过两次。