--- !ruby/object:RI::ClassDescription 
attributes: []

class_methods: []

comment: 
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Mocks and Stubs
- !ruby/struct:SM::Flow::P 
  body: RSpec will create Mock Objects and Stubs for you at runtime, or attach stub/mock behaviour to any of your real objects (Partial Mock/Stub). Because the underlying implementation for mocks and stubs is the same, you can intermingle mock and stub behaviour in either dynamically generated mocks or your pre-existing classes. There is a semantic difference in how they are created, however, which can help clarify the role it is playing within a given spec.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Mock Objects
- !ruby/struct:SM::Flow::P 
  body: Mocks are objects that allow you to set and verify expectations that they will receive specific messages during run time. They are very useful for specifying how the subject of the spec interacts with its collaborators. This approach is widely known as &quot;interaction testing&quot;.
- !ruby/struct:SM::Flow::P 
  body: Mocks are also very powerful as a design tool. As you are driving the implementation of a given class, Mocks provide an anonymous collaborator that can change in behaviour as quickly as you can write an expectation in your spec. This flexibility allows you to design the interface of a collaborator that often does not yet exist. As the shape of the class being specified becomes more clear, so do the requirements for its collaborators - often leading to the discovery of new types that are needed in your system.
- !ruby/struct:SM::Flow::P 
  body: Read Endo-Testing[http://www.mockobjects.com/files/endotesting.pdf] for a much more in depth description of this process.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Stubs
- !ruby/struct:SM::Flow::P 
  body: "Stubs are objects that allow you to set &quot;stub&quot; responses to messages. As Martin Fowler points out on his site, mocks_arent_stubs[http://www.martinfowler.com/articles/mocksArentStubs.html]. Paraphrasing Fowler's paraphrasing of Gerard Meszaros: Stubs provide canned responses to messages they might receive in a test, while mocks allow you to specify and, subsquently, verify that certain messages should be received during the execution of a test."
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Partial Mocks/Stubs
- !ruby/struct:SM::Flow::P 
  body: RSpec also supports partial mocking/stubbing, allowing you to add stub/mock behaviour to instances of your existing classes. This is generally something to be avoided, because changes to the class can have ripple effects on seemingly unrelated specs. When specs fail due to these ripple effects, the fact that some methods are being mocked can make it difficult to understand why a failure is occurring.
- !ruby/struct:SM::Flow::P 
  body: That said, partials do allow you to expect and verify interactions with class methods such as +#find+ and +#create+ on Ruby on Rails model classes.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Further Reading
- !ruby/struct:SM::Flow::P 
  body: "There are many different viewpoints about the meaning of mocks and stubs. If you are interested in learning more, here is some recommended reading:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "Mock Objects: http://www.mockobjects.com/"
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "Endo-Testing: http://www.mockobjects.com/files/endotesting.pdf"
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "Mock Roles, Not Objects: http://www.mockobjects.com/files/mockrolesnotobjects.pdf"
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "Test Double Patterns: http://xunitpatterns.com/Test%20Double%20Patterns.html"
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: "Mocks aren't stubs: http://www.martinfowler.com/articles/mocksArentStubs.html"
  type: :BULLET
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Creating a Mock
- !ruby/struct:SM::Flow::P 
  body: "You can create a mock in any specification (or setup) using:"
- !ruby/struct:SM::Flow::VERB 
  body: "  mock(name, options={})\n"
- !ruby/struct:SM::Flow::P 
  body: "The optional <tt>options</tt> argument is a <tt>Hash</tt>. Currently the only supported option is +:null_object+. Setting this to true instructs the mock to ignore any messages it hasn\xE2\x80\x99t been told to expect \xE2\x80\x93 and quietly return itself. For example:"
- !ruby/struct:SM::Flow::VERB 
  body: "  mock(&quot;person&quot;, :null_object =&gt; true)\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Creating a Stub
- !ruby/struct:SM::Flow::P 
  body: "You can create a stub in any specification (or setup) using:"
- !ruby/struct:SM::Flow::VERB 
  body: "  stub(name, stub_methods_and_values_hash)\n"
- !ruby/struct:SM::Flow::P 
  body: "For example, if you wanted to create an object that always returns &quot;More?!?!?!&quot; to &quot;please_sir_may_i_have_some_more&quot; you would do this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  stub(&quot;Mr Sykes&quot;, :please_sir_may_i_have_some_more =&gt; &quot;More?!?!?!&quot;)\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Creating a Partial Mock
- !ruby/struct:SM::Flow::P 
  body: "You don't really &quot;create&quot; a partial mock, you simply add method stubs and/or mock expectations to existing classes and objects:"
- !ruby/struct:SM::Flow::VERB 
  body: "  Factory.should_receive(:find).with(id).and_return(value)\n  obj.stub!(:to_i).and_return(3)\n  etc ...\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Expecting Messages
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:sym)\n  my_mock.should_not_receive(:sym)\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Expecting Arguments
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:sym).with(*args)\n  my_mock.should_not_receive(:sym).with(*args)\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Argument Constraints using Expression Matchers
- !ruby/struct:SM::Flow::P 
  body: "Arguments that are passed to #with are compared with actual arguments received using == by default. In cases in which you want to specify things about the arguments rather than the arguments themselves, you can use any of the Expression Matchers. They don't all make syntactic sense (they were primarily designed for use with Spec::Expectations), but you are free to create your own custom Spec::Matchers."
- !ruby/struct:SM::Flow::P 
  body: "Spec::Mocks does provide one additional Matcher method named #ducktype."
- !ruby/struct:SM::Flow::P 
  body: "In addition, Spec::Mocks adds some keyword Symbols that you can use to specify certain kinds of arguments:"
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:sym).with(no_args())\n  my_mock.should_receive(:sym).with(any_args())\n  my_mock.should_receive(:sym).with(1, an_instance_of(Numeric), &quot;b&quot;) #2nd argument can any type of Numeric\n  my_mock.should_receive(:sym).with(1, boolean(), &quot;b&quot;) #2nd argument can true or false\n  my_mock.should_receive(:sym).with(1, /abc/, &quot;b&quot;) #2nd argument can be any String matching the submitted Regexp\n  my_mock.should_receive(:sym).with(1, anything(), &quot;b&quot;) #2nd argument can be anything at all\n  my_mock.should_receive(:sym).with(1, ducktype(:abs, :div), &quot;b&quot;)\n                           #2nd argument can be object that responds to #abs and #div\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Receive Counts
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:sym).once\n  my_mock.should_receive(:sym).twice\n  my_mock.should_receive(:sym).exactly(n).times\n  my_mock.should_receive(:sym).at_least(:once)\n  my_mock.should_receive(:sym).at_least(:twice)\n  my_mock.should_receive(:sym).at_least(n).times\n  my_mock.should_receive(:sym).at_most(:once)\n  my_mock.should_receive(:sym).at_most(:twice)\n  my_mock.should_receive(:sym).at_most(n).times\n  my_mock.should_receive(:sym).any_number_of_times\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Ordering
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:sym).ordered\n  my_mock.should_receive(:other_sym).ordered\n    #This will fail if the messages are received out of order\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Setting Reponses
- !ruby/struct:SM::Flow::P 
  body: "Whether you are setting a mock expectation or a simple stub, you can tell the object precisely how to respond:"
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:sym).and_return(value)\n  my_mock.should_receive(:sym).exactly(3).times.and_return(value1, value2, value3)\n    # returns value1 the first time, value2 the second, etc\n  my_mock.should_receive(:sym).and_return { ... } #returns value returned by the block\n  my_mock.should_receive(:sym).and_raise(error)\n    #error can be an instantiated object or a class\n    #if it is a class, it must be instantiable with no args\n  my_mock.should_receive(:sym).and_throw(:sym)\n  my_mock.should_receive(:sym).and_yield(values,to,yield)\n  my_mock.should_receive(:sym).and_yield(values,to,yield).and_yield(some,other,values,this,time)\n    # for methods that yield to a block multiple times\n"
- !ruby/struct:SM::Flow::P 
  body: "Any of these responses can be applied to a stub as well, but stubs do not support any qualifiers about the message received (i.e. you can't specify arguments or receive counts):"
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.stub!(:sym).and_return(value)\n  my_mock.stub!(:sym).and_return(value1, value2, value3)\n  my_mock.stub!(:sym).and_raise(error)\n  my_mock.stub!(:sym).and_throw(:sym)\n  my_mock.stub!(:sym).and_yield(values,to,yield)\n  my_mock.stub!(:sym).and_yield(values,to,yield).and_yield(some,other,values,this,time)\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Arbitrary Handling
- !ruby/struct:SM::Flow::P 
  body: "Once in a while you'll find that the available expectations don't solve the particular problem you are trying to solve. Imagine that you expect the message to come with an Array argument that has a specific length, but you don't care what is in it. You could do this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:sym) do |arg|\n    arg.should be_an_istance_of(Array)\n    arg.length.should == 7\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: Note that this would fail if the number of arguments received was different from the number of block arguments (in this case 1).
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Combining Expectation Details
- !ruby/struct:SM::Flow::P 
  body: "Combining the message name with specific arguments, receive counts and responses you can get quite a bit of detail in your expectations:"
- !ruby/struct:SM::Flow::VERB 
  body: "  my_mock.should_receive(:&lt;&lt;).with(&quot;illegal value&quot;).once.and_raise(ArgumentError)\n"
constants: []

full_name: Spec::Mocks
includes: []

instance_methods: []

name: Mocks
superclass: 
