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:
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:
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.
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.
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'
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:
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...