[rspec-users] Help needed

Jens Carroll lists at carroll.de
Sat May 17 13:34:14 EDT 2008


Hi All,

I am new to rspec and it seems that I don't understand some basics.
I need to have a XML import which should parse through XML data
and saves all that in various mysql tables. The XML part works just
fine and I can test this with rspec. However when I try to execute

it "should find country object for DE" do

I get an error. @user.country is a one-to-many relation in the user 
table. It seems easy but I don't get it (hmmmm feels like I am still a 
newbie).

The error I get is:

'XmlImport should find country object for DE' FAILED
expected #<Country:0x..fdb71beba @name="Country_1001">, got nil (using 
.equal?)


Any help very appreciated.
Thanks
Jens

--- model ---

require 'hpricot'
class XmlImport #< ActiveRecord::Base

   attr_reader :doc, :user

   def parse_xml(file)
     @doc = Hpricot.XML(open(file))

     (@doc/:member).each do |data|
       retrieve_and_save_user(data)
     end
   end

   def retrieve_and_save_user(data)
     # email address must be unique for members
     @user = User.find_or_initialize_by_email(email)

     # save user if not existing
     if @user.new_record?
       @user.country = 
Country.find_by_short((data/:countryShort).inner_html)
     end
   end
end


---- spec -------

module XmlImportSpecHelper

   def mock_xml_import
     xml_file = RAILS_ROOT + "/spec/fixtures/import-member.xml"
     xml = File.read(xml_file)

     @xml_import = XmlImport.new
     @xml_import.should_receive(:open).exactly(1).times.
       with("any-file-name.xml").
       and_return(xml)
   end

end

describe XmlImport do

   include XmlImportSpecHelper

   before(:each) do
     mock_xml_import
     @xml_import.parse_xml("any-file-name.xml")

     @country = mock_model(Country)
     Country.stub!(:find_by_short).and_return(@country)
   end

   it "should find country object for DE" do
     Country.should_receive(:find_by_short).with("DE").and_return(@country)
     @xml_import.user.country.should equal(@country)
   end
end


More information about the rspec-users mailing list