Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Instance variable in PATCH controller specs #53

Open
ristovskiv opened this issue Mar 27, 2015 · 6 comments
Open

Instance variable in PATCH controller specs #53

ristovskiv opened this issue Mar 27, 2015 · 6 comments

Comments

@ristovskiv
Copy link

I need to ask you one quick question and you may find it pretty simple but I just couldn't find the answer for it anywhere.

When I test the controller 'patch' method I run into something that was really intriguing for me.

    describe 'PATCH #update' do
           before :each do
             @contact = create(:contact,
             firstname: 'Lawrence',
             lastname: 'Smith')
           end

           context "valid attributes" do
             it "locates the requested @contact" do
                patch :update, id: @contact, contact: attributes_for(:contact)
                expect(assigns(:contact)).to eq(@contact)
             end

             it "changes @contact's attributes" do
                patch :update, id: @contact,
               contact: attributes_for(:contact,
               firstname: 'Larry',
               lastname: 'Smith')
            # @contact.reload
              p @contact ------------ it prints #<Contact id: 1, firstname: "Lawrence", lastname: "Smith", email:   "[email protected]" ...> ???????
              p @contact.changes ---- it prints  {} (empty hash, no changes)
              p assings(:contact) == @contact --- it prints "true"
              p @contact.reload ---- it prints #<Contact id: 1, firstname: "Larry", lastname: "Smith", email:  "[email protected]" ...>
              p @contact == @contact.reload ... it prints "true"
         end
      end
   end

I understand why we should reload, because of persistance, and that @contact needs to be the same as assigns(:contact), but what I don't understand is why if I print the @contact it shows his old values. Plus if I write an example like this "expect(@contact.name).to eq ' Larry Smith' " it fails.

@ruralocity
Copy link
Member

I see this question a lot ... I'm going to write a blog post to provide some illustrated details.

@ristovskiv
Copy link
Author

Thank you for the response, looking forward for that blog post.

@ruralocity
Copy link
Member

I haven't forgotten this ... I haven't been feeling well and have been sleeping when I usually write.

@ristovskiv
Copy link
Author

No biggies, thank you really for replying and for putting the work and effort to share your knowledge. Hope you'll get well soon.

@ruralocity
Copy link
Member

@ristovskiv
Copy link
Author

Thank you so much! Great article!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants