[RSpec] Testing "validates_uniquesness of"

View: New views
2 Messages — Rating Filter:   Alert me  

[RSpec] Testing "validates_uniquesness of"

by Joseph DelCioppio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Guys,

Getting used to doing BDD, and in my current project I'm trying to
spec out my model, and in particular the fact that my model must
validate that one of its fields are unique; in this case, the
customer's email address.

Simple enough model, a customer, who only has two
fields.  :email_address and :state.

I have a model spec that does the following:

it "ensureness that the customer's email is unique" do
  customer.new :email_address => "Joe@..."
  customer2.new :email_address => "Joe @Test.com"

  customer2.should_not be_valid
end


Once I run my spec, it fails, as their is no validator in the model
yet.  However, once I add the validator it still fails.

class Customer < ActiveRecord::Base
validates_precense_of :email_address, :message => "needs to be valid."

end


The thing is, the validator actually works if I fire up the server and
try to do it myself.

Anybody know why that spec doesn't do what I expect it to?  I have a
feeling its something to do with the variables I'm creating, but could
anybody give me an example that would show me how to do this?

Thanks,

Joe
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [RSpec] Testing "validates_uniquesness of"

by Rodrigo Rosenfeld Rosas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Em 02-11-2009 04:48, Joseph.DelCioppio escreveu:

> Guys,
>
> Getting used to doing BDD, and in my current project I'm trying to
> spec out my model, and in particular the fact that my model must
> validate that one of its fields are unique; in this case, the
> customer's email address.
>
> Simple enough model, a customer, who only has two
> fields.  :email_address and :state.
>
> I have a model spec that does the following:
>
> it "ensureness that the customer's email is unique" do
>    
> customer.new :email_address => "Joe@..."

It is simple. You should have used this instead:

customer.*create* :email_address => "Joe@..."

When you call new, it doesn't instanciate in database and there is why
the second try suceeds...

Hope that helps,

Rodrigo.

__________________________________________________
Faça ligações para outros computadores com o novo Yahoo! Messenger
http://br.beta.messenger.yahoo.com/ 
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users