Sponge!

written by Jan on March 4th, 2008 @ 08:15 PM

I’ve had some complaints from non-tech people that, lately, I’ve been talking too much about technical things.. Well, too bad for you non-tech people, I’m a techie, I do this for a living and for a hobby, so once again, a technical blog post.

This time about testing. More specifically Rails testing.

When I’m testing a particular part of a function, say, in a controller, I want to stub out all things not related to that controller. Sometimes, however, this can be a pesky job. For example when you’re testing the inner works of a controller function, but don’t care about what happens to it next (say in the view). For this I have a small class, I call a Sponge, which I can return when stubbing. It takes every function and just returns itself. Thanks to this, I can do:

  it "Should set resource to the correct client" do
    Client.should_receive(:find).with("1").and_return Sponge.new

    get 'new', :client_id => 1
    assigns[:resource].should be_a_sponge
  end

In this code I just make sure that a Client is sought, with the correct arguments, and don’t care what happens to it next.

This is the code. I include this in my test helper.

class Sponge
  def self.method_missing
    Sponge.new
  end

  def method_missing method, *args
    self
  end

  def is_a_sponge?
    true
  end
end


Comments are closed

Options:

Size