【问题标题】:Rspec check return valuerspec 检查返回值
【发布时间】:2014-04-28 16:10:49
【问题描述】:

我正在学习 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

那么如何使用 to 检查该方法返回正确的“数字”变量?

【问题讨论】:

    标签: ruby-on-rails testing rspec


    【解决方案1】:

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

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

    否则 Ruby 会单独计算表达式:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多