2013-02-17 33 views
0

我显然在运行测试之前在客户模型中添加了一个名为ct_ratio的新字段。当我现在对我的发票规范运行测试,他们没有用 “没有方法错误,ct_ratio =”Rspec no方法错误分配

Customer.rb

class Customer < ActiveRecord::Base 
attr_accessible :billing_address, :customer_currency, :email, :first_name, :last_name, :mobile, :name, :payment_terms, :phase_type, :readings_attributes, :pays_vat, :ct_ratio 
    before_validation do 
self.ct_ratio ||= 1 
end 
end 

Invoice_spec

describe Invoice do 

context 'create_item_from_readings' do 
before :each do 
    @customer = FactoryGirl.create :customer 
    @reading1 = @customer.readings.create! reading1: 100, date_of_reading: 30.days.ago 
    @reading2 = @customer.readings.create! reading1: 200, date_of_reading: 20.days.ago 
    @reading3 = @customer.readings.create! reading1: 500, date_of_reading: 10.days.ago 
    @customer.stub(:unit_cost).and_return(100) 
end 

it "should create an invoice item for all reading" do 
    invoice = Invoice.new customer: @customer, invoice_date: 15.days.ago, due_date: Date.today 

    item = invoice.create_item_from_readings 
    item.rate.should == 100 
    item.amount.should == 100 * 100 
    item.description.should == "Electricity used from #{@reading1.date_of_reading.strftime('%d/%m/%Y')} to #{@reading2.date_of_reading.strftime('%d/%m/%Y')} - 100 units" 
end 

我加ct_ratio到工厂女孩

require 'factory_girl' 

FactoryGirl.define do 

factory :customer do 
name 'Test Test' 
first_name "Test" 
last_name "Test" 
mobile "00000000" 
billing_address "Kampala" 
payment_terms "Immediate" 
phase_type "Single Phase" 
customer_currency "UGX" 
pays_vat false 
email "[email protected]" 
ct_ratio 1 
end 

factory :reading do |f| 
f.reading1 100 
f.reading2 150 
f.reading3 200 
f.date_of_reading 30.days.ago 
end 

end 
+1

你“*显然*增加了一个新的领域”?你添加了它还是没有添加它? – 2013-02-17 07:26:41

+0

@shioyama我添加了一个新字段ct_ratio并将其添加到工厂女孩 – zurik 2013-02-17 10:06:12

回答

0

这是在未通过rake运行测试时发生的常见错误,并且您的迁移是否运行在测试数据库上。

运行RAILS_ENV=test rake db:migrate首先检查你的测试套件。

+0

我应该如何检查我的测试套件 – zurik 2013-02-17 14:45:47