2013-04-11 34 views
0

所以我試圖首次使用Rspec在我的應用程序上測試模型,但我似乎無法使其工作。我使用FactoryGirl創建模型的實例。這裏是我的規格:在模型測試中Rspec的should_receive的另一個問題

describe 'adding a model instance' do 
    before :each do 
     @fake = FactoryGirl.create :user 
    end 

    it 'should add a new user to the database' do 
     User.should_receive(:create).with(@fake)[email protected]) 
    end 

end 

這裏是我的模型:

class User < ActiveRecord::Base 
attr_accessible :username, :name, :email, :email_confirmation, :password, :password_confirmation, :bio 
has_many :comments #comments user has posted on ideas 
has_many :commented_ideas, :class_name => 'Idea', :through => :comments #ideas user has commented on 
has_many :ideas #ideas user has posted 

validates :password, :email, :confirmation => true 
validates :username, :email, :password, :name, :presence => true 
# validates :username, :email, :unique => true 
end 

和錯誤,我越來越:

Failure/Error: User.should_receive(:create!).with('username' => 'testUser', 'user_id' => "1").and_return(true) 
    (<User(id: integer, username: string, name: string, email: string, password: string, created_at: datetime, updated_at: datetime, bio: text) (class)>).create!({"username"=>"testUser", "user_id"=>"1"}) 
     expected: 1 time 
     received: 0 times 

是否有人可以解釋爲什麼這不工作?

回答

1

User#create在您的代碼中沒有被調用,因此未滿足的期望。我認爲你的誤解是由於真正的課堂與工廠混淆。我不知道FactoryGirl內部是如何創建記錄的,但它看起來不是#create。

相關問題