Passionate Engineer
Passionate Engineer

Reputation: 10412

Rspec stub why use at all

Why would you use stub given that you should be testing real classes anyways?

I know there are cases that you don't want to test other classes and not go through its other associated class methods but I still think it's better to use real methods.

I can only really see a benefit when you want to quickly skip one method's other associated tasks and return the end result to be used in tests

Are there other benefits though that we should be considering?

(In addition to above I also think stub is risky aswell since your code can change as it evolves and may generate different output to what it is generating in the tests)

Upvotes: 1

Views: 163

Answers (1)

David Weiser
David Weiser

Reputation: 5195

It depends on the test that you are performing. For unit tests, where you are only testing a single class, stubs are beneficial.

As an example, assume you are testing a class which sends an email when some other object finishes the did_it! operation:

describe Emailer do
  context ".send_email" do
    it "sends an email if another object 'did_it!'" do
      obj = Obj.new
      Emailer.send_email(obj).should == true # the email sends successfully
    end
  end
end

In this case, if obj.did_it! is a super expensive operation, or it could fail intermittently, this test could have issues.

However, in this test we only care that Emailer.send_email runs correctly when obj.did_it! returns true--we do not care that the obj.did_it! method works, because that is not what we are testing.

So, we use stubs to say, "assuming that obj.did_it! succeeds, does Emailer.send_email actually send email?":

describe Emailer do
  context ".send_email" do
    it "sends an email if another object 'did_it!'" do
      obj = stub(:did_it! => true)
      Emailer.send_email(obj).should == true # the email sends successfully
    end
  end
end

Upvotes: 1

Related Questions