1

I have something like this in my RSpec file (it works fine for me):

    before do
      @attr = attributes_for(:album)      
      post :create, album: @attr
    end
    it { expect(Album.exists?(@attr)).to be_true }
    it { expect(response).to redirect_to(assigns[:album]) }

Is it possible to make it without attributes_for(:album) in a separate line and @attr variable ? Or maybe even other better way..

2
  • Your question seems a bit unclear? what is your goal? Commented Mar 31, 2014 at 19:41
  • Get rid of @attr temp. variable. I believe it can be done more.. Ruby :) Commented Mar 31, 2014 at 19:45

1 Answer 1

4

No need of before block or @attr variable, you could change the example to something like this:

it "creates a album in the database" do
   expect { post :create, album: attributes_for(:album) }.to change(Album, :count).by(1)
end
Sign up to request clarification or add additional context in comments.

4 Comments

yes but then how does he retrieve that Album in the test to make sure it was created correctly?
I saw that way somewhere too but.. is it good enough to assume that it was saved correctly ? and I have more it tests there so I would like to keep it in before block.
@pawel7318 Yes, it is good enough to test the record was saved correctly. And it is normal way of testing controllers create method.
@pawel7318 As for the more tests goes, lets discuss this on chat as I need to understand few things chat.stackoverflow.com/rooms/48530/ror

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.