ruby - Rspec stub why use at all -


why use stub given should testing real classes anyways?

i know there cases don't want test other classes , not go through other associated class methods still think it's better use real methods.

i can see benefit when want skip 1 method's other associated tasks , return end result used in tests

are there other benefits though should considering?

(in addition above think stub risky aswell since code can change evolves , may generate different output generating in tests)

it depends on test performing. unit tests, testing single class, stubs beneficial.

as example, assume testing class sends email when other object finishes did_it! operation:

describe emailer   context ".send_email"     "sends email if object 'did_it!'"       obj = obj.new       emailer.send_email(obj).should == true # email sends     end   end end 

in case, if obj.did_it! super expensive operation, or fail intermittently, test have issues.

however, in test care emailer.send_email runs correctly when obj.did_it! returns true--we not care obj.did_it! method works, because not testing.

so, use stubs say, "assuming obj.did_it! succeeds, emailer.send_email send email?":

describe emailer   context ".send_email"     "sends email if object 'did_it!'"       obj = stub(:did_it! => true)       emailer.send_email(obj).should == true # email sends     end   end end 

Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -