0

I'm working with a REST approach with rails and I'm not using the notation assign "@user" inside the controllers.

Example of my controller:

def create
    user = User.new
    user.star = true
    user.save
    ...
end

For a classic rails app this the solution:

expect(assigns(:user).star).to be_present

But unlike the use of @, I need to access the user

How can I achieve that? Thanks!

2
  • 3
    The best approach would be to not test the controller. Put this in a service and test the service. Have the controller use the service. Then test everything is wired together correctly with an integration test. Commented Jul 25, 2014 at 22:17
  • @AlexPeachey Thanks. I'm kind of new in tests, but will I not step in this problem again? Could you guide me with some links to achieve it? Commented Jul 25, 2014 at 23:40

1 Answer 1

2

Since you're creating the user you could just load the last one created:

describe 'create action' do
  let(:user) { User.last }

  it 'sets star to true' do
    post :create, user: {}
    expect(user.star).to be_truthy
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Model.last!! Genius haha

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.