Hello Frank,<br><br>From my understanding these are the roles of should_receive and stub.<br><br>should_receive checks to make sure that a method or a property is called. To this you can specify the arguments that it gets called (.with()), what it returns (.and_return) and how many times this happens (.once, .twice etc). <br>
<br>stub on the other hand is a place holder for functions calls that have been tested already or are Rails defaults, which don&#39;t need to be tested. stubs are used in conjunction with mock_models, in order to provide for the functions or properties that are needed for the code to run, up to the test point. <br>
<br>As far as mock_model and Factory_girl, I can say that mock_models are a much more lightweight structure than Factory_girl object. When testing controllers I try to use only mock_models. As far as models, I use Factory_girl only when dealing with the object being tested. The other complementary objects are mocked by using mock_model. <br>
<br><div class="gmail_quote">On Wed, Feb 3, 2010 at 2:00 AM, Frank Lakatos <span dir="ltr">&lt;<a href="mailto:me@franklakatos.com">me@franklakatos.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi guys, been following for about 3 weeks, first question -<br>
<br>
I&#39;ve been spending the last couple of months learning RSpec and Cucumber and I&#39;m just finally starting to see the &quot;big picture&quot;, at least I think I am. But I&#39;ve got some questions I was hoping you guys can clear up. I&#39;m sure this has been asked a lot, so please bare with me. Just let me know when my logic is wrong or when I&#39;m way off course. You guys have been a kind group, so I&#39;m not to concerned with sounding foolish.<br>

<br>
I&#39;ve been using .should_receive and .stub to let my spec know that my controller is going to be making a method call. I know .should_receive will fail when you don&#39;t call that method (or if you call it more times than you said you would) and I use stub when It&#39;s going to get called an unpredictable amount of times, like in a before block. To me, it sounds like I don&#39;t really know what the difference is, and I just have some understanding of there symptoms.<br>

<br>
When i need to talk about a model, I usually use mock_model. I have Factory_girl, but I&#39;ve heard that you should be careful when using Factory girl, as it ties you down to the requirements of the attributes, and not the overall functionality (although, that statement may not be true in itself). So normally, I use Factories in Cucumber, as that is concerned with more higher level usage features, and mock_models for controller/model specs.<br>

<br>
Alot of times, however, I feel like I&#39;m doing way to much work to get the whole thing working. Maybe I am, maybe I&#39;m not, only way is to show off what I&#39;m doing.<br>
<br>
app/controller/projects_controller#create<br>
<br>
  def create<br>
      @client = current_user.company.clients.find(params[:project][:client_id])<br>
      @project = @client.projects.build(params[:project])<br>
      if @client.save<br>
        flash[:notice] = &quot;Added: #{@<a href="http://project.name" target="_blank">project.name</a>}&quot;<br>
      else<br>
        render :new<br>
      end<br>
  end<br>
<br>
<br>
spec/controller/projects_controller_spec<br>
<br>
describe ProjectsController do<br>
    describe &quot;POST &#39;create&#39;&quot; do<br>
<br>
    before do<br>
      @current_user = mock_model(User)<br>
      controller.stub(:current_user).and_return @current_user<br>
      @company = mock_model(Company)<br>
      @current_user.should_receive(:company).and_return @company<br>
      @clients = mock(&quot;Client List&quot;)<br>
        @company.should_receive(:clients).and_return @clients<br>
    end<br>
<br>
    describe &quot;when client is found&quot; do<br>
<br>
      before do<br>
        @client = mock_model(Client)<br>
        @clients.should_receive(:find).and_return @client<br>
      end<br>
<br>
      describe &quot;on successful save&quot; do<br>
<br>
        before do<br>
          @projects = mock_model(ActiveRecord)<br>
          @client.should_receive(:projects).and_return @projects<br>
          @project = mock_model(Project)<br>
          @projects.should_receive(:build).and_return @project<br>
          @client.should_receive(:save).and_return true<br>
          @project.should_receive(:name).and_return &quot;New Project&quot;<br>
        end<br>
<br>
        it &quot;should set up the flash&quot; do<br>
          post &quot;create&quot;, {:project =&gt; {:client_id =&gt; 1}}<br>
          flash[:notice].should_not be_nil<br>
        end<br>
<br>
       end<br>
<br>
    end<br>
<br>
  end<br>
<br>
<br>
end<br>
<br>
<br>
Let me know how it sounds, and it looks like I&#39;m doing so far<br>
<br>
Thanks,<br>
Frank<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
rspec-users mailing list<br>
<a href="mailto:rspec-users@rubyforge.org" target="_blank">rspec-users@rubyforge.org</a><br>
<a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Andrei Erdoss<br>