我有这些验证:
it "should have 100 adventure points" do
user = User.new
user.adventure_points.should == 100
end
it "should be level 1" do
user = User.new
user.level.should == 1
end
it "should have 10 sapphires" do
user = User.new
user.sapphires.should == 10
end
然后,我也在迁移中:
t.string :name, :limit => 20
t.integer :strength_points, :default => 0
t.integer :dexterity_points, :default => 0
t.integer :magic_points, :default => 0
t.integer :accuracy_points, :default => 0
t.integer :health_points, :default => 0
t.integer :level, :default => 1
t.integer :adventure_points, :default => 100
t.integer :sapphires, :default => 10
t.integer :duel_wins, :default => 0
t.integer :duel_losses, :default => 0
t.string :image_url
t.integer :strength
当我运行rspec时,我得到:
A new User
is not valid without a name
is not valid without a first class
is not valid without a password
should have 100 adventure points
should be level 1 (FAILED - 1)
should have 10 sapphires (FAILED - 2)
Failures:
1) A new User should be level 1
Failure/Error: user.level.should == 1
expected: 1
got: nil (using ==)
# ./spec/models/user_spec.rb:28
2) A new User should have 10 sapphires
Failure/Error: user.sapphires.should == 10
expected: 10
got: nil (using ==)
# ./spec/models/user_spec.rb:33
为什么它在级别,蓝宝石上呈现这些错误,但它对于adventure_points是否正常?此外,如果我打开一个控制台并执行User.new,我会按预期获得默认值。什么在这里肆虐?
答案 0 :(得分:1)
在保存模型之前,不会设置数据库默认值。尝试:
it "should have 100 adventure points" do user = User.create user.adventure_points.should == 100 end
答案 1 :(得分:1)
我自己找到了解决方案,非常偷偷摸摸。我不得不运行rake:test:prepare因为测试数据库没有夹具数据并且没有准备好:P