[rspec-users] Failure creating model in spec setup not reported?

Ashley Moran work at ashleymoran.me.uk
Mon Mar 26 09:59:42 EDT 2007


Hi

I've just tracked down a wierd error that AFAICT is caused by an  
error not being raised in the setup:

     context "An Asset" do
       setup do
         @provider = Provider.create(:name => "Provider1")
         @product = Product.new(:name => "Product1", :provider =>  
@provider)
         @applicant = Applicant.new(:first_name => "Fred", :last_name  
=> "Flinstone", :email => "fred at flinstone.com")

         @asset = Asset.new(:value => BigDecimal("250"))
       end

       specify "should have many quote parameters" do
         @asset.applicant = @applicant

         quote_parameters_1 = QuoteParameters.new(:term =>  
36, :applicant => @applicant, :product => @product)
         quote_parameters_2 = QuoteParameters.new(:term =>  
48, :applicant => @applicant, :product => @product)

         @asset.should respond_to(:quote_parameters)
         @asset.quote_parameters.should respond_to(:<<)
         @asset.quote_parameters << quote_parameters_1
         @asset.quote_parameters << quote_parameters_2

         @asset.save.should == true

         asset_reloaded = Asset.find(@asset.id)
         asset_reloaded.quote_parameters.map { |p|  
p.term }.sort.should == [ 36, 48 ]
       end
     end

Regardless of whether this is a good way to spec the association or  
(probably) not, it fails on "@asset.save.should == true" with this  
error:
should have many quote parameters
PGError: ERROR:  null value in column "provider_id" violates not-null  
constraint
: INSERT INTO products ("name", "provider_id", "default_term",  
"maximum_resolicitation_period", "type", "gap_cover_type_id",  
"created_at") VALUES('Product1', NULL, NULL, NULL, NULL, NULL,  
'2007-03-26 14:42:29.872399')


However the probably is actually caused by the first line of the  
setup, which is missing a mandatory parameter (using validations):

@provider = Provider.create(:name =>  
"Provider1", :external_identifier => "external-id")

(I was using "new" but changed it to "create" to highlight the issue)

Why would this not fail sooner?


Thanks
Ashley




More information about the rspec-users mailing list