<commit_before>= rspec-spies

Behold, the Test Spies pattern! http://xunitpatterns.com/Test%20Spy.html

Some other mocking frameworks support this. In particular, rr, notamock, and jferris's fork of mocha. It'd be great to see support for it in rspec's mocking library as well.

Why not just switch to another framework? Well, because migration is a huge hassle when you have a sizeable number of specs, in part to subtle varations in the framework. Even something like notamock that was meant to work with rspec out of the box... has its differences. Therefore, using the existing rspec mocking framework, with a small enhancements, is pretty idyllic.

In general, I think it should work by doing your usual stub!, followed running the code under test, and then verifying with a matcher. The matcher should probably be in line with the syntax of mocking with should_receive. Here's a short example:


    describe "index" do
      before do
        @post = stub_model(Post)
        Post.stub!(:find).and_return(@post)
        get :show, :id => @post.id
      end
    
      it "should find post by id" do
        Post.should have_received(:find).with(@post.id)
      end
    end

To get started, you just need to:

 * Run: gem install rspec-spies
 * Update spec_helper to include this line: require 'rspec-spies'


== Before and after test spies

If you think this simple case is redudant, consider this extended example without using test spies (note: these are using shoulda's rspec matchers):

    describe "show"  do
      before do
        @post = stub_model(Post)
        # always stub find, so most of the specifications will work
        # alternatively, this could be in each 'it' block that doesn't have a should_receive
        Post.stub!(:find).and_return(@post)
      end
    
      it "should respond with success" do
        get :show, :id => @post.id
        <commit_msg>Updated readme to include gem install with gemcutter source.<commit_after>= rspec-spies

Behold, the Test Spies pattern! http://xunitpatterns.com/Test%20Spy.html

Some other mocking frameworks support this. In particular, rr, notamock, and jferris's fork of mocha. It'd be great to see support for it in rspec's mocking library as well.

Why not just switch to another framework? Well, because migration is a huge hassle when you have a sizeable number of specs, in part to subtle varations in the framework. Even something like notamock that was meant to work with rspec out of the box... has its differences. Therefore, using the existing rspec mocking framework, with a small enhancements, is pretty idyllic.

In general, I think it should work by doing your usual stub!, followed running the code under test, and then verifying with a matcher. The matcher should probably be in line with the syntax of mocking with should_receive. Here's a short example:


    describe "index" do
      before do
        @post = stub_model(Post)
        Post.stub!(:find).and_return(@post)
        get :show, :id => @post.id
      end
    
      it "should find post by id" do
        Post.should have_received(:find).with(@post.id)
      end
    end

To get started, you just need to:

 * Run: gem install rspec-spies --source http://gems.gemcutter.org
 * Update spec_helper to include this line: require 'rspec-spies'


== Before and after test spies

If you think this simple case is redudant, consider this extended example without using test spies (note: these are using shoulda's rspec matchers):

    describe "show"  do
      before do
        @post = stub_model(Post)
        # always stub find, so most of the specifications will work
        # alternatively, this could be in each 'it' block that doesn't have a should_receive
        Post.stub!(:find).and_return(@post)
      end
    
      it "should respond with success" do
        get :show, :id => @post.id
        
{'fraction_non_alphanumeric': 0.06687402799377916, 'fraction_numerical': 0.0010368066355624676, 'mean_word_length': 3.2037037037037037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1586464', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1028, 'n_words': 518}