我正在测试我的方法 populate()
是否返回一个非空值(它确实返回了一个 > 0 的整数)但在正确编写它时遇到了麻烦。我有:
describe House::Room do
describe '.populate' do
let(:info) {
$info = {"people"=>
{"name"=>"Jordan",
"last_name"=>"McClalister"}}
}
it 'should return an integer > 0' do
expect(House::Room.populate(info)).not_to eq(nil)
end
end
end
最佳答案
您需要将 let 分配更改为:
describe House::Room do
describe '.populate' do
let(:info) {"people"=>
{"name"=>"Jordan",
"last_name"=>"McClalister"}
}
it 'should return an integer > 0' do
expect(House::Room.populate(info)).not_to be(nil)
end
end
end
这应该使您的代码按预期工作。
但是,如果您想更具体一些,您也可以使用另一个匹配器,例如“be_within”,或者在同一个测试中编写多个 expect 语句,例如“expect to be an integer”、“expect to be greater than 0” ' 等等...在“it” block 中可以包含的期望语句的数量没有限制,只有满足所有期望时测试才会通过。 (也就是说,我认为最佳做法是将其拆分为单独的测试。)
关于ruby-on-rails - 测试方法是否返回 `non-null` 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38753908/