2011-01-24 132 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