我想在注册时测试新用户,而不是admin(默认设置:迁移时为false)。到目前为止,这是我的user_spec.rb:
require 'spec_helper'
describe User do
before { @user = FactoryGirl.build(:user) }
subject { @user }
it { should respond_to(:username) }
it { should respond_to(:email) }
it { should respond_to(:admin) }
describe "When username is too short" do
before { @user.username = 'ab' }
it { should_not be_valid }
end
describe "When username is too long" do
before { @user.username = 'a' * 26 }
it { should_not be_valid }
end
describe "Username is present" do
before { @user.username = " " }
it { should_not be_valid }
end
end
我试过
it "should not be admin" do
expect { @user.admin }.to be_false
end
但它返回:
expected: false value
got: #<Proc:0x007ffc4b544a58@"
我怀疑这是因为我正在运行基于工厂的测试,但如果我明确地将'admin false'放入工厂,它将无法测试默认值。
如何测试默认模型值?我是否应该通过注册与水豚,然后测试该用户?
答案 0 :(得分:2)
您需要将@user.admin
作为参数传递,而不是在块中传递,如:
expect(@user.admin).to be(false)
传递块适用于要评估操作副作用的情况,例如更新数据库,引发错误等。