[rspec-users] When to use Factories, Mock Models, Mocks & Stubs

Andrei Erdoss erdoss at gmail.com
Wed Feb 3 00:53:10 EST 2010


Hello Frank,

>From my understanding these are the roles of should_receive and stub.

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).

stub on the other hand is a place holder for functions calls that have been
tested already or are Rails defaults, which don'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.

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.

On Wed, Feb 3, 2010 at 2:00 AM, Frank Lakatos <me at franklakatos.com> wrote:

> Hi guys, been following for about 3 weeks, first question -
>
> I've been spending the last couple of months learning RSpec and Cucumber
> and I'm just finally starting to see the "big picture", at least I think I
> am. But I've got some questions I was hoping you guys can clear up. I'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'm way off course. You guys have been a kind group,
> so I'm not to concerned with sounding foolish.
>
> I'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't call that method (or if you call it more times than you
> said you would) and I use stub when It's going to get called an
> unpredictable amount of times, like in a before block. To me, it sounds like
> I don't really know what the difference is, and I just have some
> understanding of there symptoms.
>
> When i need to talk about a model, I usually use mock_model. I have
> Factory_girl, but I'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.
>
> Alot of times, however, I feel like I'm doing way to much work to get the
> whole thing working. Maybe I am, maybe I'm not, only way is to show off what
> I'm doing.
>
> app/controller/projects_controller#create
>
>  def create
>      @client =
> current_user.company.clients.find(params[:project][:client_id])
>      @project = @client.projects.build(params[:project])
>      if @client.save
>        flash[:notice] = "Added: #{@project.name}"
>      else
>        render :new
>      end
>  end
>
>
> spec/controller/projects_controller_spec
>
> describe ProjectsController do
>    describe "POST 'create'" do
>
>    before do
>      @current_user = mock_model(User)
>      controller.stub(:current_user).and_return @current_user
>      @company = mock_model(Company)
>      @current_user.should_receive(:company).and_return @company
>      @clients = mock("Client List")
>        @company.should_receive(:clients).and_return @clients
>    end
>
>    describe "when client is found" do
>
>      before do
>        @client = mock_model(Client)
>        @clients.should_receive(:find).and_return @client
>      end
>
>      describe "on successful save" do
>
>        before do
>          @projects = mock_model(ActiveRecord)
>          @client.should_receive(:projects).and_return @projects
>          @project = mock_model(Project)
>          @projects.should_receive(:build).and_return @project
>          @client.should_receive(:save).and_return true
>          @project.should_receive(:name).and_return "New Project"
>        end
>
>        it "should set up the flash" do
>          post "create", {:project => {:client_id => 1}}
>          flash[:notice].should_not be_nil
>        end
>
>       end
>
>    end
>
>  end
>
>
> end
>
>
> Let me know how it sounds, and it looks like I'm doing so far
>
> Thanks,
> Frank
>
>
>
>
>
>
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Andrei Erdoss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100203/623e88be/attachment-0001.html>


More information about the rspec-users mailing list