title: Testing before_create method in rspec and rails 3
body: <p>I've looked into some tutes and all I saw were old posts on how to test before_create. Also it seems like they're all just testing that before_create was called i.e.:</p>
  
  <pre><code>@user = User.new
  @user.should_receive(:method_name_called_by_before_create)
  @user.send(:before_create) (sometimes they just do @user.save)
  </code></pre>
  
  <p>I want to actually test that my method worked and that it had assigned(and saved the variables) after creating the record.</p>
  
  <p>Here are my models:</p>
  
  <p>user.rb</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_one :character, :dependent =&gt; :destroy
  
    after_create :generate_character
  
    private
      def generate_character
        self.create_character(:name =&gt; "#{email}'s avatar")
      end
  end
  </code></pre>
  
  <p>and character.rb</p>
  
  <pre><code>class Character &lt; ActiveRecord::Base
  
    belongs_to :user
  
    before_create :generate_character
  
    private
      def generate_character
        response = api_call
        #API CALL HERE
  
        #set object attributes here
        self.stat1 = calculate_stat1(response) + 5
        self.stat2 = calculate_stat2(response) + 5
        self.stat3 = calculate_stat3(response) + 5            
      end
  
      def api_call
        return api_call_response
      end
  
  end
  </code></pre>
  
  <p>I want to test that generate character indeed set the attributes without going online and calling the API call. Is this possible with rspec? I have a fixture of a json response so I was hoping I can stub out generate character and then use the fake response for testing.</p>
  
  <p>Here's my character.spec:</p>
  
  <pre><code>describe Character do
    before(:each) do
      Character.any_instance.stub!(:api_call).and_return(fake_response.read)
      @user = Factory(:user)
      @character = @user.character
      puts  @character.inspect
  end
  
  def fake_response
    File.open("spec/fixtures/api_response.json")
  end
  </code></pre>
  
  <p>It prints out only 5 for each of the character's stats. Also I did a <code>puts response</code> in the generate_character method in character.rb and it still prints out the "real" api call. </p>
  
  <p>I managed to do a puts in fake_response and it does goes through there but it also goes through the "real" api_call after, which makes the stub obsolete. How do I get through this?</p>