RSpec respond_to不起作用

时间:2016-11-21 09:59:28

标签: ruby-on-rails rspec

这是我测试多态关系的代码片段。

我写了如下测试。

require 'rails_helper'

RSpec.describe Proration, type: :model do
  let(:invoice_item) { FactoryGirl.create(:proration_invoice_item) }
  subject { :invoice_item }

  it { should be_valid }
  it { should respond_to(:total_amount) }
  it { should respond_to(:invoice) }
  it { should respond_to(:itemable) }
end

但这错误了 故障:

  1) Proration should be valid
     Failure/Error: it { should be_valid }

     NoMethodError:
       undefined method `valid?' for :invoice_item:Symbol
     # ./spec/models/proration_spec.rb:7:in `block (2 levels) in <top (required)>'

  2) Proration Proration should respond to #total_amount
     Failure/Error: end
       expected :invoice_item to respond to :total_amount
     # ./spec/models/proration_spec.rb:16:in `block (3 levels) in <top (required)>'

  3) Proration Proration should respond to #invoice
     Failure/Error: end
       expected :invoice_item to respond to :invoice
     # ./spec/models/proration_spec.rb:17:in `block (3 levels) in <top (required)>'

  4) Proration Proration should respond to #itemable
     Failure/Error: Unable to find matching line in /Users/toshikiinami/Desktop/billing/spec/models/proration_spec.rb
       expected :invoice_item to respond to :itemable
     # ./spec/models/proration_spec.rb:18:in `block (3 levels) in <top (required)>'

有什么想法来解决这个问题? (模型的respond_to不起作用。)

1 个答案:

答案 0 :(得分:2)

您使用invoice_item定义了let,但是subject使用了符号。你想要的是那个对象invoice_item,所以

subject{ :invoice_item }

应改为

subject{ invoice_item }