Ruby on rails 如何使用Rspec测试Rails中的模型默认值

Ruby on rails 如何使用Rspec测试Rails中的模型默认值,ruby-on-rails,testing,rspec,capybara,Ruby On Rails,Testing,Rspec,Capybara,我想测试一个新用户在注册时是否不是admin(在迁移中默认设置为false)。以下是我的用户_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 { s

我想测试一个新用户在注册时是否不是admin(在迁移中默认设置为false)。以下是我的用户_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@"
应为:假值

got:#您需要将
@user.admin
作为参数传递,而不是在块中传递,如:

expect(@user.admin).to be(false)

传递块适用于您希望评估操作副作用的情况,例如更新数据库、引发错误等。

admin
布尔值吗?如果是这样,请尝试
期望{@user.admin?}.to_false
@DamienRoche admin是一个布尔值,而.admin?当我没主意的时候也会有同样的结果。这是一个奇怪的返回值。尝试Pry,并添加
绑定。在该行上方Pry
,然后查询
@user
实例,看看发生了什么。@DamienRoche Pry显示了完整的用户模型,其规格与my Factorys.rb中的规格相同。默认情况下,Admin为false。感谢您尝试帮助。顺便说一句,
[1]pry(#)>@user=>#