2013-07-15 188 views
0

随机值我在我的模型的一个方法,该方法返回一个随机行用于测试使用RSpec

Intention.offset(rand(Intention.count)).first 

它工作正常,但我该如何使用RSpec测试吗?

回答

1

你可以在你的代码做到这一点:

Kernel.rand(Intention.count) 

,并在您的规格:

let(:intention_count) { 3 } 

Intention.stub(:count).and_return(intention_count) 
Kernel.stub(:rand).with(intention_count).and_return(0) # will return 0 

使用Kernel类基本上,我们呼吁兰特能够stub该方法返回我们想要什么。