2011-01-24 165 views
0

我有以下型號:簡單Rspec的測試失敗

module CgCart 
    class ShippingInfo < ActiveRecord::Base 
    set_table_name 'cg_cart.shipping_infos' 
    has_many :order, :class_name => "CgCart::Order" 
    accept_nested_attributes_for :orders, :allow_destroy => true 
    validates_presence_of :name, :street, :city, :country, :zip, :state 

def to_s 
    retval = <<-formatted_addr 
    #{self.name} 
    #{self.street} 
    #{self.city} #{self.state} #{self.zip} 
    #{self.country} 
     formatted_addr 
     retval.upcase 
    end 
    end 
end # module 

我寫一個規範的測試。它是這樣的:

require File.dirname(__FILE__) + '/../../spec_helper' 
describe CgCart::ShippingInfo do 
    context "when first created" do 
     it "should be empty" do 
      @shipping_info = CgCart::ShippingInfo.new 
      @shipping_info.should be_empty 
     end 
    end 
end 

當我運行規範的測試,我得到以下錯誤:

1) 
NoMethodError in 'CgCart::ShippingInfo when first created should be empty' 
undefined method `be_empty' for #<Spec::Matchers::PositiveOperatorMatcher:0x105f165c0> 
./spec/models/cg_cart/shipping_info_spec.rb:6: 

Finished in 0.015 seconds 

1 example, 1 failure 

這似乎像它應該非常直截了當。關於爲什麼測試失敗的任何想法?

+1

您還沒有爲`CgCart :: ShippingInfo`實例定義`empty?`方法。你想要測試什麼?可能是`@ shipping_info.orders.should be_empty`? – jpemberthy 2011-01-24 18:50:39

回答

0

我試圖使用空的地方,我應該使用零。

@shipping_info.should be_nil