Tumblelog by Soup.io
Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

November 19 2010

alloy

When not to use mocking/stubbing?

When you are testing a method like the following contrived example:

  def make_pony_eyes_pink
    before, Pony.eye_color = Pony.eye_color, :pink
    yield
  ensure
    Pony.eye_color = before
  end

Don’t do it with expectations such as these:

  it "changes the eye color to pink for the duration of the block" do
    Pony.expects(:eye_color=).with(:pink)
    pony.make_pony_eyes_pink {}
    Pony.eye_color.should.be :black
  end

Your expectation would still succeed if the implementation of the method would change to reset the eye color before yielding the block:

  def make_pony_eyes_pink
    before, Pony.eye_color = Pony.eye_color, :pink
    Pony.eye_color = before
    yield
  end

Therefore, check that what you describe is actually what you get:

  it "changes the eye color to pink for the duration of the block" do
    eye_color_during_block = nil
    pony.make_pony_eyes_pink do
      eye_color_during_block = Pony.eye_color
    end
    eye_color_during_block.should.be :pink
    Pony.eye_color.should.be :black
  end

Astute readers will note that I do not make the assertion inside the block, because that would mean the assertion could potentially *not* run without making the test fail.
alloy
In a Objective-C application which uses garbage collection, like a MacRuby app, any objects instantiated in a NIB will get collected as well. Unless you keep a ‘strong’ reference to them from an object reachable from a ‘root’ object, such as NSApplication.sharedApplication (NSApp).

As I normally make my AppController instance the NSApp's delegate (‘strong’ reference) right from the start, I didn't notice this and it kept me busy for a good hour.
Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.

Don't be the product, buy the product!

Schweinderl