Rspec 3 - Testing assign in controller action
<p>Currently I have the following custom action in my controller:</p>

<pre><code>def set_active
  current_user.active_meal_plan = @meal_plan
  current_user.save

  respond_with @meal_plan, location: -&gt; { meal_plans_path }
end
</code></pre>

<p>The controller action works as it is expected. However, I am a bit at a loss on how to test the <code>current_user.active_meal_plan = @meal_plan</code> part using Rspec.</p>

<p>This is how my test looks like:</p>

<pre><code>RSpec.describe MealPlansController, type: :controller do
  let(:user) { FactoryGirl.create(:user, :admin) }

  before :each do
    # Sign in with Devise as an admin user
    @request.env["devise.mapping"] = Devise.mappings[:admin]
    sign_in user

    # Bypass CanCan's authorization
    allow_any_instance_of(CanCan::ControllerResource).to      receive(:load_and_authorize_resource){ nil }
  end

  # ...

  describe "PUT #set_active" do
    let(:meal_plan) { FactoryGirl.create(:meal_plan, user: user) }

    it "assigns the requested meal plan to @meal_plan" do
      put :set_active, id: meal_plan
      expect(assigns(:meal_plan)).to eq(meal_plan)
    end

    it "sets the requested meal plan as the user's active meal plan" do
      put :set_active, id: meal_plan
      expect(assigns(user.active_meal_plan)).to eq(meal_plan)
    end

    it "redirects to the meal plans view" do
      put :set_active, id: meal_plan
      expect(response).to redirect_to meal_plans_path
    end
  end
</code></pre>

<p>All the tests pass except the 2nd one.</p>

<p>Here is my User model:</p>

<pre><code>class User &lt; ActiveRecord::Base
  # ..
  has_many :meal_plans, dependent: :destroy
  belongs_to :active_meal_plan, class_name: 'MealPlan'
end
</code></pre>

<p>And my User factory:</p>

<pre><code>FactoryGirl.define do
    factory :user do
        first_name 'John'
        last_name 'Doe'
        address 'San Francisco Bay Area'
        email { Faker::Internet.email }
        password "password"
        password_confirmation "password"
        pricing_plan { FactoryGirl.create(:pricing_plan) }
        active_meal_plan nil
    end

    trait :user do
      after(:create) {|user| user.add_role(:user)}
    end

    trait :admin do
      after(:create) {|user| user.add_role(:admin)}
    end
end
</code></pre>

<p>Moreover, apparently <code>assigns</code> will be deprecated in Rails 5. Actually, <a href="https://github.com/rails/rails/issues/18950#issuecomment-77924771" rel="nofollow">controller tests will be removed</a>. From what I gather, according to the discussion, it seems that I should not be testing such things....</p>

<p>Anyways, I would like to make this test pass.</p>