When I try to execute the following example, I get an error message: /usr/local/lib/ruby/gems/1.8/gems/mocha-0.5.4/lib/mocha/object.rb:40: in `expects'': undefined method `stub'' for nil:NilClass (NoMethodError) from test8.rb:5 What could be the reason? I tried with the latest Mocha Ruby gem, and I also tried it with the Rails plugin. The example: require ''rubygems'' require ''mocha'' some_time = Time.at(0) Time.expects(:now).returns(some_time) puts Time.now -- Felix E. Klee Jabber/Google Talk: feklee at jabber.org, SIP: 9779619 at sipgate.de ICQ: 158124695, Yahoo!: feklee, AIM: felix.klee at inka.de Gizmo: felixklee, Skype: felix.klee
When I try to execute the following example, I get an error message: /usr/local/lib/ruby/gems/1.8/gems/mocha-0.5.4/lib/mocha/object.rb:40: in `expects'': undefined method `stub'' for nil:NilClass (NoMethodError) from test8.rb:5 What could be the reason? I tried with the latest Mocha Ruby gem, and I also tried it with the Rails plugin. The example: require ''rubygems'' require ''mocha'' some_time = Time.at(0) Time.expects(:now).returns(some_time) puts Time.now -- Felix E. Klee Jabber/Google Talk: feklee at jabber.org, SIP: 9779619 at sipgate.de ICQ: 158124695, Yahoo!: feklee, AIM: felix.klee at inka.de Gizmo: felixklee, Skype: felix.klee
You need to try it from within a test framework like Test::Unit or RSpec. Something like this... require ''test/unit'' require ''rubygems'' require ''mocha'' class ExampleTest < Test::Unit::TestCase def test_time_now some_time = Time.at(0) Time.expects(:now).returns(some_time) puts Time.now end end -- James. http://blog.floehopper.org
At Wed, 29 Aug 2007 14:29:58 +0100, James Mead wrote:> You need to try it from within a test framework like Test::Unit or > RSpec. Something like this...This works, but if a test framework is necessary, then Mocha seems to be uninteresting for me: I want to use it when demoing a Ruby on Rails application, i.e. in a development environment, or maybe - during the alpha phase - in a production environment. Should I better forge my own Time.now function? -- Felix E. Klee Jabber/Google Talk: feklee at jabber.org, SIP: 9779619 at sipgate.de ICQ: 158124695, Yahoo!: feklee, AIM: felix.klee at inka.de Gizmo: felixklee, Skype: felix.klee
On 29/08/2007, Felix E. Klee <felix.klee at inka.de> wrote:> > This works, but if a test framework is necessary, then Mocha seems to be > uninteresting for me: I want to use it when demoing a Ruby on Rails > application, i.e. in a development environment, or maybe - during the > alpha phase - in a production environment. > > Should I better forge my own Time.now function? >This isn''t really what Mocha was intended for - it''s really intended for doing "interation-based testing" [1]. The main technical reason you need to use it within a test is that it hooks into the point at which each test ends to put any class it has modified back into its original state. You could look at Mocha::TestCaseAdapter class and write your own equivalent, but if the only thing you need it for is stubbing Time.now - I''d recommend you create your own fake version of Time.now. I hope that helps. 1. http://www.martinfowler.com/articles/mocksArentStubs.html -- James. http://blog.floehopper.org http://tumble.floehopper.org
At Wed, 29 Aug 2007 14:51:35 +0100, James Mead wrote:> I hope that helps.It does, thanks! Felix -- Felix E. Klee Jabber/Google Talk: feklee at jabber.org, SIP: 9779619 at sipgate.de ICQ: 158124695, Yahoo!: feklee, AIM: felix.klee at inka.de Gizmo: felixklee, Skype: felix.klee