所以我第一次尝试使用Rspec来测试我的应用程序上的模型,但我似乎无法让它工作。我正在使用FactoryGirl来创建模型的实例。这是我的规范:
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).and_recieve@fake)
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
有人可以解释为什么这不起作用吗?
答案 0 :(得分:1)
User#create
未在您的代码中的任何位置调用,因此未满足的期望。我认为你的误解是由于真正的阶级与工厂的混淆。我不知道FactoryGirl在内部做什么来创建记录,但它似乎不是#create。