使用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
答案 0 :(得分:1)
好:
load_and_authorize_resource
替换为authorize_resource
或者:
@user = User.new()
(带有不必要的括号)你肯定在这里做过两次。