开发者

Best Way to Test Boolean Values in Rspec

开发者 https://www.devze.com 2023-03-28 18:13 出处:网络
Looking to开发者_如何转开发 get some opinions here. What is the best way to check boolean values with RSPEC I have seen it done a few different ways:

Looking to开发者_如何转开发 get some opinions here.

What is the best way to check boolean values with RSPEC I have seen it done a few different ways:

myvar.should == true
myvar.should be true
myvar.should be

Also I usually only care about the duck value, that is, if it evaluates to true / false then I don't care what its actual value is...


Here's the difference between "== true" and "be(true)":

describe true do
  it { should be(true) }
  it { should be_true }
end

describe 'true' do
  it { should_not be(true) }
  it { should be_true }
end

Which basically means that if you only care if a value evaluates to true then you want to use == true


I ended up doing the opposite logic:

expect([true, false]).to include myvar


In my advise the best way is to add rspec_boolean to your Gemfile in the group :development, :test:

gem 'rspec_boolean'

And than use it like:

expect(val.is_something?).to be_boolean

0

精彩评论

暂无评论...
验证码 换一张
取 消