[rspec-users] Mock or Stub strategy for validates_uniqueness_of

Daniel N has.sox at gmail.com
Tue Jul 24 09:49:47 EDT 2007


On 7/24/07, Ashley Moran <work at ashleymoran.me.uk> wrote:
>
>
> 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 =>
> @dealer)
>
>      @provider = Provider.create!(:name => "Provider1")
>      @product = Product.create!(:name => "Product1", :provider =>
> @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 =>
> 250)
>      Purchase.create!(:quote => @quote, :amount_financed => 200)
>    end
>
>    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
>    end
> end
>
> 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.)
>
> Ashley


That looks like the kind of thing I was initially thinking of, but I started
to think that having all those objects being created in a non-related spec
could lead to trouble.  If I change what makes any of those objects valid,
these supposedly unrelated specs will then break.

I want to try and keep everything seperate, but in this case it seems that
things are interdependent.

Cheers
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070724/53e8aedd/attachment.html 


More information about the rspec-users mailing list