[rspec-users] Mock or Stub strategy for validates_uniqueness_of

Ashley Moran work at ashleymoran.me.uk
Tue Jul 24 09:42:20 EDT 2007

On 24 Jul 2007, at 14:07, Daniel N wrote:

> Any hints as to how to do this without using fixtures?

I did this literally minutes ago.  I don't use fixtures at all,  
ever.  Instead I put the data in the before block, eg:

describe Purchase, "assigned to a Quote that already has a saved  
purchase" do
   before(:each) do
     @dealer = Dealer.create!(:name =>  
"Dealer1", :external_identifier => "D123")
     @site = Site.create!(:hostname => "my.host.name", :dealer =>  

     @provider = Provider.create!(:name => "Provider1")
     @product = Product.create!(:name => "Product1", :provider =>  
     @applicant = Applicant.create!(:first_name => "Fred", :last_name  
=> "Flinstone", :email => "fred at flinstone.com", :telephone_home =>  
"01782 123456")
     @quote_parameters = QuoteParameters.create!(:term =>  
36, :applicant => @applicant, :product => @product, :site => @site)
     @quote = InsuranceQuote.create!(:valid_until =>  
1.days.from_now, :quote_parameters => @quote_parameters, :premium =>  
     Purchase.create!(:quote => @quote, :amount_financed => 200)

   it "should be invalid" do
     purchase = Purchase.new(:quote =>  
@quote, :amount_collected_by_card => "value")
     purchase.should_not be_valid
     purchase.errors.on(:quote_id).should_not be_nil

Comments welcome on whether this is a good way to do this.  (I know  
that the above doesn't test my database unique constraint, which I  
don't do in this case come to think of it, but I try to keep DB specs  
separate, hard as that may be sometimes.)


More information about the rspec-users mailing list