Search code examples
rspecrspec-rails

How to test that a new record in the controller has an attribute


I know I can use assigns to see if instance variables are assigned to something, but how do I test that another property has been set?

class UsersController < ApplicationController
  def new
     @user = User.new
     @user.name = "Tom Lehrer"
  end
end

describe UsersController do
  it "assigns a user" do
    get :new
    expect(assigns(:user)).to be_a_new(User)
  end

  it "sets the new users name" do
    # ??
  end
end

Solution

  • You can just test against assigns(:user):

    expect(assigns(:user).name).to eq("Expected Name")