Created
November 23, 2011 21:48
-
-
Save garybernhardt/1390018 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make a user that we'll add a book to. | |
user = User.create! | |
controller.stub(:current_user) { user } | |
# This prints []. The book list is empty. | |
p user.books | |
# Expect a book to be added to the user. This fails (see below) | |
expect { | |
post :create, :id => asin | |
}.to change { user.books }.from([]).to([book]) | |
# Failure/Error: expect { | |
# books should have initially been [], but was [#<Book id: 1, asin: "the-asin", title: "Hamlet", authors: "Bill Shakes", pages: 0, is_ebook: false, image_url: nil, icon_url: nil, details_url: "http://example.com/hamlet", created_at: "2011-11-23 21:47:45", updated_at: "2011-11-23 21:47:45", editorial_review: nil>] | |
# This seems wrong. `user.books` *is* initially []. Help? I'm running RSpec 2.7.1 on Ruby 1.8.7 and Rails 3.0.9. It also happens with RSpec 2.5. |
But RSpec contains a special case to dup Enumerables, and user.books.is_a?(Enumerable) is true, though the when/case match in RSpec isn't firing for some reason. This is hurting my brain.
Oh - I forgot we added that :(
Regardless, this should work: }.to change{ user.books.first }.from(nil).to(book)
Ahh, yes! That's much less confusing than a manual dup. Thanks.
It's not as nice as what you had originally, but it's a fair trade-off to get it to work.
I think change { user.books.reload }.from([]).to([book])
works too. But yuck.
Nope, reloading the books doesn't help (neither does reloading the user). dup
ing the books does. Confusingly, to_a
ing the books doesn't (I'd expect it to create a new array each time).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
user.books returns the same object both times, and that object gets updated in place :(