Forum: Ruby on Rails why is my rspec controller test for get method not working?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
661ac1900b7d3884589d7c81a66e93ca?d=identicon&s=25 Peter (Guest)
on 2013-11-17 07:10
(Received via mailing list)
Hello Everyone,

Why is my controller test in the get method not working?

This is practically a copy from the rails guides and it works in my
test. The render test works... it's only the assigns portion that
to work.

I'm studying Rails, but am focusing on rspec because Rails by itself is
easy and I feel that I don't want to write anything without rspec being
right next to it, even if I have to write the tests after. I have gone
through several tutorials already:
Michael Hartl's, Pragmatic Studio, and Code School.

So now, I just need to do it and gain proficiency. Thank you.

661ac1900b7d3884589d7c81a66e93ca?d=identicon&s=25 Peter (Guest)
on 2013-11-17 08:52
(Received via mailing list)

I corrected my code, but am still getting this:

> 18 examples, 1 failure
> Failed examples:
> rspec ./spec/controllers/accounts_controller_spec.rb:18 #
> AccountsController Get new assigned @account
> Randomized with seed 52138
5b50fec1e5bf39bb4d75b199130882f7?d=identicon&s=25 Josh Jordan (Guest)
on 2013-11-17 16:00
(Received via mailing list)
I would have to contest that you didn't quite correct it then, eh? :p

You wrote an expectation that says "I expect @account to be an array of
accounts that contains a single, specific instance of Account." Why are
looking for an array? Your controller code looks right - it assigns
@account to be a new instance Account.

So you, you have two problems here. The one I described you can
this way:

expected = [account]
puts "Expected class: #{expected.class}"
puts "Actual class: #{assigns(:account).class}"
expect( assigns(:account) ).to eq( expected )

The second is that you're comparing two new instances of Account, which
will not be equal.

puts ==

You probably just want to check what type of object was assigned to
@account and that it is unpersisted. Something like (and my syntax might
off here):

actual = assigns(:account)
expect(actual).to be_a(Account)
expect(actual).to be_new_record
661ac1900b7d3884589d7c81a66e93ca?d=identicon&s=25 Peter (Guest)
on 2013-11-21 03:34
(Received via mailing list)
Thanks for that. I didn't know there was such a 'be_a()' matcher. Where
could I read more about the available permutations of 'be_'? Thank you.

Also, I read again the Rails Guides on The Four Hashes of the
Apocalypse. I
thought assigns() would be a hash no matter what. I understand now that
was a hash in the index action because I gave it a hash. But the way it
being used in my new action, it's not a hash, it's just a new Account
object that is empty. Thank you.
This topic is locked and can not be replied to.