I have this model
class User < ActiveRecord::Base
after_create :create_in_remote
def create_in_remote
if self.remote_id.nil?
req_url = "api/create/user/#{self.username}"
response = Remote.execute_request(req_url)
if response
self.update_attribute(:remote_id, response["id"]) if response["id"]
end
end
end
end
In my spec I try to do this
require 'spec_helper'
describe User do
before { @user = FactoryGirl.build(:user) }
subject { @user }
it "has_remote_id" do
@user.save! && @user.reload
expect(@user.remote_id).not_to be_nil
end
end
but never passes the test but the object is always created on the remote api. If I want to see the value of the attribute remote_id of the @user object is always null. If I change build to create in the factory, i can see in the log that the attribute is updated, but when use in the test it is null and fail the test.
The syntax in your test looks wrong. Rspec introduced the
expectsyntax so that instead of this:you can do this:
You're mixing
expectwithshouldso I imagine this is causing your test to fail.Here's a good blog post about Rspec's new expectation syntax