I'm really struggling with Rspec DSL! I read a lot on SO and across the internet, so I'm posting my particular issue because my feeble mind can't get to the solution.
Have a post method in my controller that updates a user's email. Works fine, but I'm struggling with the spec because all I'm getting are undefined methods for NilClass (even though I've tried stubbing every object and method, etc).
users_controller.rb
def update_user_email @user = User.find_by_id(params[:id]) new_email = params[:user][:new_email].downcase.strip user_check = User.find_by_email('new_email') if user_check.blank? @user.email = new_email @user.save flash[:notice] = "Email updated to #{new_email}" else flash[:alert] = "This email is already being used by someone else!" end respond_with @user do |format| format.html { redirect_to admin_user_path(@user) } format.json { head :no_content } end end Here's the spec I'm trying to write. What test should I be writing, if not this, and what can I do to prevent undefined method on NilClass errors!
users_controller_spec.rb
describe Admin::UsersController do let!(:user) { FactoryGirl.create(:user, password: 'oldpass', email: '[email protected]') } ... describe "admin actions for each user" do it "resets user email" do post :update_user_email, {user: {new_email: '[email protected]'} } response.status.should == 200 end end ... end And the error:
Admin::UsersController admin actions for each user resets user email Failure/Error: post :update_user_email, {user: {new_email: '[email protected]'} } NoMethodError: undefined method `email=' for nil:NilClass