When you upgrade rails 3.2 -> 4.0. Do not pass the tests, and the error is the following:
Failure/Error: post :create, :user => user_params
ActiveRecord::ActiveRecordError:
can not touch on a new record object
As this problem can be solved?
describe "POST create" do
def do_post
User.should_receive(:new).with(HashWithIndifferentAccess.new(user_params)).and_return(user)
binding.pry
post :create, :user => user_params
end
let(:profile_params) { {:first_name => "John", :last_name => "Bobson", :country => "US"} }
let(:user_params) { {:vid => "12345", :username => "johnbobson", :email => "[email protected]", :user_profile_attributes => profile_params} }
let(:user) { User.new(user_params) }
context "user" do
subject { do_post; user }
its(:invited_at) { should == Date.today.to_time.utc }
its(:invitation_code) { should_not be_nil }
end
end
The reason for this method.
def update_last_active_at
current_user.touch(:last_active_at) if current_user
end
That error is coming about most likely because current_user has not been saved in the database. Rails cannot touch a record that hasn't been saved yet.
To verify this, just check if the current user is a new record before trying to touch it.
def update_last_active_at
if !current_user.new_record?
current_user.touch(:last_active_at) if current_user
end
end