#create tests fail when I add FriendlyId to my model

50 views
Skip to first unread message

Walter Lee Davis

unread,
Aug 4, 2011, 3:35:15 PM8/4/11
to rubyonra...@googlegroups.com
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

Kendall Gifford

unread,
Aug 4, 2011, 4:13:59 PM8/4/11
to rubyonra...@googlegroups.com
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.
 

Walter Lee Davis

unread,
Aug 4, 2011, 4:22:40 PM8/4/11
to rubyonra...@googlegroups.com

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

Walter Lee Davis

unread,
Aug 4, 2011, 4:45:32 PM8/4/11
to rubyonra...@googlegroups.com

On Aug 4, 2011, at 4:13 PM, Kendall Gifford wrote:

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-
> ta...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
> .

Reply all
Reply to author
Forward
0 new messages