Tom Milewski
2011-Feb-23 15:29 UTC
[rspec-users] Validations aren''t being performed when running ''rspec spec''
Hello, When I run ''rspec spec/models'' everything runs beautifully. When I run ''rspec spec/controllers'' everything also runs beautifully. When I run ''rspec spec'' the models seem to forget that the records need to pass the validations before being saved to the database. This is consistent across all model tests which are checking validations. Model: validates_presence_of :name, :location, :email... Test: it "should ensure that name is present" do Factory.build(:public_agent, :name => nil).should have(1).error_on(:name) end All of these tests do not return errors and do add the invalid records to the database. Has anyone else had this happen? Suggestions? Thanks!
Craig Demyanovich
2011-Feb-24 13:48 UTC
[rspec-users] Validations aren''t being performed when running ''rspec spec''
On Wed, Feb 23, 2011 at 10:29 AM, Tom Milewski <tmilewski at gmail.com> wrote:> Model: > > validates_presence_of :name, :location, :email... > > Test: > > it "should ensure that name is present" do > Factory.build(:public_agent, :name => nil).should > have(1).error_on(:name) > end > > All of these tests do not return errors and do add the invalid records > to the database. > > Has anyone else had this happen? Suggestions?Though I''m still using RSpec 1, I do use FactoryGirl. Factory.build only instantiates a new record; it does not save it. Thus, the example that you shared should not create a record. Are you sure that the invalid records that you''re seeing in the database are from this example? Regards, Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110224/837b3415/attachment.html>
David Chelimsky
2011-Feb-24 13:48 UTC
[rspec-users] Validations aren''t being performed when running ''rspec spec''
On Feb 23, 2011, at 9:29 AM, Tom Milewski wrote:> Hello, > > When I run ''rspec spec/models'' everything runs beautifully. > When I run ''rspec spec/controllers'' everything also runs beautifully. > When I run ''rspec spec'' the models seem to forget that the records > need to pass the validations before being saved to the database. > > This is consistent across all model tests which are checking > validations. > > Model: > > validates_presence_of :name, :location, :email... > > Test: > > it "should ensure that name is present" do > Factory.build(:public_agent, :name => nil).should have(1).error_on(:name)Factory.build ^^ doesn''t even save records to the database in the first place, so it would be surprising if this were the example that is causing trouble. Do you have any other specs besides models and controllers? helpers, mailers, requests, etc? I''ve seen cases in which data was set up in before(:all) (which does not run in a transaction and is therefore not rolled back), and uniqueness validations would fail in other places. What specific failure messages are you seeing?> end > > All of these tests do not return errors and do add the invalid records > to the database. > > Has anyone else had this happen? Suggestions? > > Thanks! > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersCheers, David
Tom Milewski
2011-Feb-24 19:07 UTC
[rspec-users] Validations aren''t being performed when running ''rspec spec''
Thanks for the replies. Here''s the error I''m seeing when running all specs (.build and .create): Agent while creating should ensure that name is present Failure/Error: Factory(:public_agent, :name => nil).should have(1).error_on(:name) expected 1 error on :name, got 0 # ./spec/models/agent_spec.rb:35:in `block (3 levels) in <top (required)>'' Here''s what I''m seeing when running model specs with .create: Agent while creating should ensure that name is present Failure/Error: Factory(:public_agent, :name => nil).should have(1).error_on(:name) ActiveRecord::RecordInvalid: Validation failed: Name can''t be blank # ./spec/models/agent_spec.rb:35:in `block (3 levels) in <top (required)>'' Everything passes when running model specs with .build. Thanks again! On Feb 24, 8:48?am, David Chelimsky <dchelim... at gmail.com> wrote:> On Feb 23, 2011, at 9:29 AM, Tom Milewski wrote: > > > > > > > > > > > Hello, > > > When I run ''rspec spec/models'' everything runs beautifully. > > When I run ''rspec spec/controllers'' everything also runs beautifully. > > When I run ''rspec spec'' the models seem to forget that the records > > need to pass the validations before being saved to the database. > > > This is consistent across all model tests which are checking > > validations. > > > Model: > > > validates_presence_of :name, :location, :email... > > > Test: > > > it "should ensure that name is present" do > > ? ? ?Factory.build(:public_agent, :name => nil).should have(1).error_on(:name) > > Factory.build ^^ doesn''t even save records to the database in the first place, so it would be surprising if this were the example that is causing trouble. > > Do you have any other specs besides models and controllers? helpers, mailers, requests, etc? I''ve seen cases in which data was set up in before(:all) (which does not run in a transaction and is therefore not rolled back), and uniqueness validations would fail in other places. > > What specific failure messages are you seeing? > > > end > > > All of these tests do not return errors and do add the invalid records > > to the database. > > > Has anyone else had this happen? Suggestions? > > > Thanks! > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > Cheers, > David > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Craig Demyanovich
2011-Feb-24 19:26 UTC
[rspec-users] Validations aren''t being performed when running ''rspec spec''
On Thu, Feb 24, 2011 at 2:07 PM, Tom Milewski <tmilewski at gmail.com> wrote:> Thanks for the replies. > > Here''s the error I''m seeing when running all specs (.build > and .create): > > Agent while creating should ensure that name is present > Failure/Error: Factory(:public_agent, :name => nil).should > have(1).error_on(:name) > expected 1 error on :name, got 0 > # ./spec/models/agent_spec.rb:35:in `block (3 levels) in <top > (required)>'' > > Here''s what I''m seeing when running model specs with .create: > > Agent while creating should ensure that name is present > Failure/Error: Factory(:public_agent, :name => nil).should > have(1).error_on(:name) > ActiveRecord::RecordInvalid: > Validation failed: Name can''t be blank > # ./spec/models/agent_spec.rb:35:in `block (3 levels) in <top > (required)>'' > > Everything passes when running model specs with .build. >Have you tried with Agent.new and/or Agent.create? Maybe factory_girl is doing something strange. Also, what versions of rails, rspec-rails, rspec and factory_girl are you using? Craig -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110224/f255bd9f/attachment.html>
Tom Milewski
2011-Feb-24 19:34 UTC
[rspec-users] Validations aren''t being performed when running ''rspec spec''
I should also note that if I replace ".should have(1).error_on(:name)" with, simply, ".should be_valid". The same issues occur. All specs (except when only running models) pass when they shouldn''t be. On Feb 24, 8:48?am, David Chelimsky <dchelim... at gmail.com> wrote:> On Feb 23, 2011, at 9:29 AM, Tom Milewski wrote: > > > > > > > > > > > Hello, > > > When I run ''rspec spec/models'' everything runs beautifully. > > When I run ''rspec spec/controllers'' everything also runs beautifully. > > When I run ''rspec spec'' the models seem to forget that the records > > need to pass the validations before being saved to the database. > > > This is consistent across all model tests which are checking > > validations. > > > Model: > > > validates_presence_of :name, :location, :email... > > > Test: > > > it "should ensure that name is present" do > > ? ? ?Factory.build(:public_agent, :name => nil).should have(1).error_on(:name) > > Factory.build ^^ doesn''t even save records to the database in the first place, so it would be surprising if this were the example that is causing trouble. > > Do you have any other specs besides models and controllers? helpers, mailers, requests, etc? I''ve seen cases in which data was set up in before(:all) (which does not run in a transaction and is therefore not rolled back), and uniqueness validations would fail in other places. > > What specific failure messages are you seeing? > > > end > > > All of these tests do not return errors and do add the invalid records > > to the database. > > > Has anyone else had this happen? Suggestions? > > > Thanks! > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > Cheers, > David > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Tom Milewski
2011-Feb-25 16:58 UTC
[rspec-users] Validations aren''t being performed when running ''rspec spec''
Thanks for all of your insight guys. As it turned out the culprit was ".any_instance.stubs(:valid?).returns(false)" which was being run in the controllers. I changed some things around any now everything is working as expected. I was using the following: Rails 3.0.0 RSpec 2.4.0 RSpec Rails 2.4.1 Factory Girl 2.0.0.beta1 Thanks again! On Feb 24, 2:26?pm, Craig Demyanovich <cdemyanov... at gmail.com> wrote:> On Thu, Feb 24, 2011 at 2:07 PM, Tom Milewski <tmilew... at gmail.com> wrote: > > Thanks for the replies. > > > Here''s the error I''m seeing when running all specs (.build > > and .create): > > > Agent while creating should ensure that name is present > > ? ? Failure/Error: Factory(:public_agent, :name => nil).should > > have(1).error_on(:name) > > ? ? ? ?expected 1 error on :name, got 0 > > ? ? # ./spec/models/agent_spec.rb:35:in `block (3 levels) in <top > > (required)>'' > > > Here''s what I''m seeing when running model specs with .create: > > > Agent while creating should ensure that name is present > > ? ? Failure/Error: Factory(:public_agent, :name => nil).should > > have(1).error_on(:name) > > ? ? ?ActiveRecord::RecordInvalid: > > ? ? ? Validation failed: Name can''t be blank > > ? ? # ./spec/models/agent_spec.rb:35:in `block (3 levels) in <top > > (required)>'' > > > Everything passes when running model specs with .build. > > Have you tried with Agent.new and/or Agent.create? Maybe factory_girl is > doing something strange. > > Also, what versions of rails, rspec-rails, rspec and factory_girl are you > using? > > Craig > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users