我刚刚添加了Rolify gem,并且正在运行一些 rspec 测试。
2个测试如下:
describe "roles" do
before(:each) do
@user = FactoryGirl.create(:user)
end
it "should not approve incorrect roles" do
@user.add_role :moderator
@user.has_role? :admin
should be_false
end
it "should approve correct roles" do
@user.add_role :moderator
@user.has_role? :moderator
should be_true
end
end
测试结果是:
1) User roles should not approve incorrect roles
Failure/Error: should be_false
expected: false value
got: #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: nil, confirmation_sent_at: nil, name: nil, position: nil, location: nil, admin: false, archived: false, public_email: false, created_at: nil, updated_at: nil>
# ./spec/models/user_spec.rb:70:in `block (3 levels) in <top (required)>'
Finished in 1.37 seconds
7 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:67 # User roles should not approve incorrect roles
Randomized with seed 13074
工厂.rb
FactoryGirl.define do
factory :user do
sequence(:name) {|n| "Example User #{n}"}
sequence(:email) {|n| "email#{n}@program.com" }
position "Regular"
location "Anywhere, USA"
public_email false
password "foobar"
password_confirmation "foobar"
confirmed_at Time.now
end
end
第一个测试如何以 nil 对象失败,但第二个测试通过?
编辑
进一步检查后,任何测试should be_true
通过,任何测试should be_false
失败,无论添加的角色是否与检查的角色匹配。