Testing methods called on yielded object

Posted by Todd R on Stack Overflow See other posts from Stack Overflow or by Todd R
Published on 2010-05-11T14:51:31Z Indexed on 2010/05/11 14:54 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

I have the following controller test case:

def test_showplain
  Cleaner.expect(:parse).with(@somecontent)
  Cleaner.any_instance.stubs(:plainversion).returns(@returnvalue)

  post :showplain, {:content => @somecontent}

end

This works fine, except that I want the "stubs(:plainversion)" to be an "expects(:plainversion)".

Here's the controller code:

def showplain
   Cleaner.parse(params[:content]) do | cleaner |
      @output = cleaner.plainversion
   end
end

And the Cleaner is simply:

class Cleaner

   ### other code and methods ###

   def self.parse(@content) 
     cleaner = Cleaner.new(@content)
     yield cleaner
     cleaner.close
   end

   def plainversion
      ### operate on @content and return ###
   end

end

Again, I can't figure out how to reliably test the "cleaner" that is made available from the "parse" method. Any suggestions?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about mocha