test block

MyClass.find_each do |instance_of_my_class|
  do_stuff_here_on(instance_of_my_class)
end

MyClass.stub(:find_each).and_yield(one_mock_instance_of_my_class).and_yield(two_mock_instance_of_my_class)


Advertisements

stub method to return different values based on the times it is called

You can stub a method to return different values each time it’s called;

@family.stub(:location).and_return('first', 'second', 'other')

So the first time you call @family.location it will return ‘first’, the second time it will return ‘second’, and all subsequent times you call it, it will return ‘other’.

source: http://stackoverflow.com/questions/5947999/can-rspec-stubbed-method-return-different-values-in-sequence

stub a method and return the value depending on the parameter

stub! can accept a block. The block receives the parameters; the return value of the block is the return value of the stub:

class Interface
end

describe Interface do
  it "should have a stub that returns its argument" do
    interface = Interface.new
    interface.stub!(:get_trace) do |arg|
      arg
    end
    interface.get_trace(123).should eql 123
  end
end

source: http://stackoverflow.com/questions/5938049/rspec-stubbing-return-the-parameter

let and let!

My conclusion is that both let and let! cached value within examples, but not cross examples. If another example calls it, both of them will be executed. The only difference is that let is lazy, which means it won’t be executed if it is not invoked, but that is not true for let!.

“let” is only evaluated when invoked, and keeps the same value within a scope

https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/helper-methods/let-and-let

(:let!) is forcefully evaluated before each method call.

$count = 0
describe "let!" do
  invocation_order = []

  let!(:count) do
    invocation_order << :let!
    $count += 1
  end

  it "calls the helper method in a before hook" do
    invocation_order << :example
    invocation_order.should == [:let!, :example]
    count.should eq(1)
  end

  it "calls the helper method again" do
    count.should eq(2)
  end
end

Rspec note

  • describe() is alias for context(); use the former for things, and the latter for context.
  • run “–format documentation” in the command-line option to get full documentation.
  • pending for Rspec, WIP for cucumber, avoid those examples from being run but with a reminder in the output

as_null_object

it tells the double to only call the methods we tell it to expect and ignore any other methods

Example:
it “should receive sth.a”
a = double(‘model’)
model = Model.new(a)
a.should_receive(:method_a).with(sth.a)
model.start
end

def start
method_a(sth.a)
method_a(sth.b)
end

+++++++++++++++++++++++++++++++++++++
if we add another example
it “should receive sth.b”
a = double(‘model’)
model = Model.new(a)
a.should_receive(:method_a).with(sth.b)
model.start
end

Both example will be failed
+++++++++++++++++++++solution ++++++++++++++++++
change a = double(‘model’) to a = double(‘model’).as_null_object