Walter Lee Davis
2011-Aug-04 19:35 UTC
#create tests fail when I add FriendlyId to my model
I am doing controller testing, and I can''t seem to get the create method test to pass when friendly_id is added to the mix. If I comment it out of the model, the tests all pass perfectly. The moment I add it back in, the error looks like this: 1) Error: test_create_valid(BrandsControllerTest): FriendlyId::BlankError: FriendlyId::BlankError app/controllers/brands_controller.rb:16:in `create'' test/functional/brands_controller_test.rb:27:in `test_create_valid'' Here''s the relevant line in brand.rb: has_friendly_id :name, :use_slug => true, :approximate_ascii => true Here''s the test (using mocha): def test_create_valid Brand.any_instance.stubs(:valid?).returns(true) post :create assert_redirected_to brand_url(assigns(:brand)) end Here''s the fixture: one: name: MyString image_file_name: MyString image_content_type: MyString image_file_size: 1 image_updated_at: 2011-08-03 10:49:03 tease: MyText description: MyText All other tests pass, and if I comment out has_friendly_id, the create tests pass as well. Does this ring any bells for anyone? Thanks in advance, Walter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Kendall Gifford
2011-Aug-04 20:13 UTC
Re: #create tests fail when I add FriendlyId to my model
On Thursday, August 4, 2011 1:35:15 PM UTC-6, Walter Lee Davis wrote:> > I am doing controller testing, and I can''t seem to get the create > method test to pass when friendly_id is added to the mix. If I comment > it out of the model, the tests all pass perfectly. The moment I add it > back in, the error looks like this: > > 1) Error: > test_create_valid(BrandsControllerTest): > FriendlyId::BlankError: FriendlyId::BlankError > app/controllers/brands_controller.rb:16:in `create'' > test/functional/brands_controller_test.rb:27:in `test_create_valid'' > > Here''s the relevant line in brand.rb: > > has_friendly_id :name, :use_slug => true, :approximate_ascii => true > > Here''s the test (using mocha): > > def test_create_valid > Brand.any_instance.stubs(:valid?).returns(true) > post :create > assert_redirected_to brand_url(assigns(:brand)) > end >I''m gonna take a stab at this and say it''s because you''re not posting a name parameter to the create action. Therefor, the create action is trying to create a new Brand w/out a name. Since you''re using the friendly_id gem and configuring the Brand model to use its "name" field as the friendly id, you must provide a name else you get FriendlyId::BlankError.> Here''s the fixture: > > one: > name: MyString > image_file_name: MyString > image_content_type: MyString > image_file_size: 1 > image_updated_at: 2011-08-03 10:49:03 > tease: MyText > description: MyText >Doesn''t really matter what you have in your fixtures since you''re simulating a request that results in a call to your #create action in your controller which will create a new (blank) Brand record.> All other tests pass, and if I comment out has_friendly_id, the create > tests pass as well. Does this ring any bells for anyone? > > Thanks in advance, > > Walter >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/vVodkyMZiGIJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Lee Davis
2011-Aug-04 20:22 UTC
Re: Re: #create tests fail when I add FriendlyId to my model
On Aug 4, 2011, at 4:13 PM, Kendall Gifford wrote:> I''m gonna take a stab at this and say it''s because you''re not > posting a name parameter to the create action. Therefor, the create > action is trying to create a new Brand w/out a name. Since you''re > using the friendly_id gem and configuring the Brand model to use its > "name" field as the friendly id, you must provide a name else you > get FriendlyId::BlankError. > Here''s the fixture: > > one: > name: MyString > image_file_name: MyString > image_content_type: MyString > image_file_size: 1 > image_updated_at: 2011-08-03 10:49:03 > tease: MyText > description: MyText > > Doesn''t really matter what you have in your fixtures since you''re > simulating a request that results in a call to your #create action > in your controller which will create a new (blank) Brand record.I''ve tried all sorts of variations on this, along the lines of post :create, :name => ''MyString'' and b = Brand.any_instance b.stubs(:valid?).returns(true) b.stubs(:name).returns(''MyString'') post :create and I''ve gotten the identical error. Can you show me the correct way to write this, such that I define the value for name at the correct moment? Thanks, Walter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Lee Davis
2011-Aug-04 20:45 UTC
Re: Re: #create tests fail when I add FriendlyId to my model
On Aug 4, 2011, at 4:13 PM, Kendall Gifford wrote:> On Thursday, August 4, 2011 1:35:15 PM UTC-6, Walter Lee Davis > wrote:I am doing controller testing, and I can''t seem to get the > create > method test to pass when friendly_id is added to the mix. If I comment > it out of the model, the tests all pass perfectly. The moment I add it > back in, the error looks like this: > 1) Error: > test_create_valid(BrandsControllerTest): > FriendlyId::BlankError: FriendlyId::BlankError > app/controllers/brands_controller.rb:16:in `create'' > test/functional/brands_controller_test.rb:27:in > `test_create_valid'' > > Here''s the relevant line in brand.rb: > > has_friendly_id :name, :use_slug => true, :approximate_ascii => > true > > Here''s the test (using mocha): > > def test_create_valid > Brand.any_instance.stubs(:valid?).returns(true) > post :create > assert_redirected_to brand_url(assigns(:brand)) > end > > > I''m gonna take a stab at this and say it''s because you''re not > posting a name parameter to the create action. Therefor, the create > action is trying to create a new Brand w/out a name. Since you''re > using the friendly_id gem and configuring the Brand model to use its > "name" field as the friendly id, you must provide a name else you > get FriendlyId::BlankError. > Here''s the fixture: > > one: > name: MyString > image_file_name: MyString > image_content_type: MyString > image_file_size: 1 > image_updated_at: 2011-08-03 10:49:03 > tease: MyText > description: MyText > > Doesn''t really matter what you have in your fixtures since you''re > simulating a request that results in a call to your #create action > in your controller which will create a new (blank) Brand record. > > All other tests pass, and if I comment out has_friendly_id, the create > tests pass as well. Does this ring any bells for anyone? >Okay, thanks to Kendall and the Test Prescriptions book, I think I have it fixed. This works: def test_create_valid b = Brand.any_instance b.stubs(:name).returns(''MyString'') b.stubs(:valid?).returns(true) post :create assert_redirected_to brand_url(assigns(:brand)) end For some reason, having stubs(:valid?).returns(true) before stubs(:name).returns(''MyString'') made it continue to fail, while putting the name in first worked. Posting this so I can find it again later when I forget... Walter> Thanks in advance, > > Walter > > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/vVodkyMZiGIJ > . > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > .-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.