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

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?
https://github.com/haloflightleader/bank/blob/0269...

This is practically a copy from the rails guides and it works in my
index
test. The render test works... it's only the assigns portion that
refuses
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.

p
661ac1900b7d3884589d7c81a66e93ca?d=identicon&s=25 Peter (Guest)
on 2013-11-17 08:52
(Received via mailing list)
https://github.com/haloflightleader/bank/commit/88...

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
you
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
illustrate
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 Account.new == Account.new

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
be
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
it
was a hash in the index action because I gave it a hash. But the way it
is
being used in my new action, it's not a hash, it's just a new Account
object that is empty. Thank you.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.