Rspec检查返回值

时间:2014-04-28 16:10:49

标签: ruby-on-rails testing rspec

我在rails rspec中学习测试。我有这部分测试:

subject(:service) { described_class.new() }
context 'when sth' do
  it 'should sth' do
    expect ( service.call(@params) ).to eq(0)
  end
end

服务的方法调用返回号码。但我得到这样的东西:

故障:

 1) Module::Registration when sth should sth
 Failure/Error: expect ( service.call(@params) ).to eq(2)
 NoMethodError:
   undefined method `to' for 2:Fixnum
 # ./spec/module/registration_spec.rb:22:in `block (3 levels) in <module:Module>'

Finished in 2.48 seconds
1 example, 1 failure

那么如何检查该方法是否正确&#34; Number&#34;变量使用到?

1 个答案:

答案 0 :(得分:3)

您必须删除expect(之间的空格:

expect( service.call(@params) ).to eq(0)
#    ^^

否则Ruby会单独评估表达式:

( service.call(@params) ).to
( 2 ).to
#=> undefined method `to' for 2:Fixnum