Posts

Showing posts with the label mocking

Minitest - calling a mocked instance method from within another instance method

Minitest - calling a mocked instance method from within another instance method I've come across some weird behavior in Minitest::Mock and can't figure out what's the reason behind it. Minitest::Mock Say I have this class A that has a method b that calls method c : b c class A def b c end def c 1 end end I would like to mock the c method to return 2 instead of 1: c require 'minitest/autorun' a = Minitest::Mock.new(A.new) a.expect(:c, 2) But for some reason b still returns 1: b > a.b => 1 Obviously calling c directly will work: c > a.c => 2 > a.c MockExpectationError: No more expects available for :c: Why is the expectation on c not being invoked when calling it from within the instance? c 1 Answer 1 This is how Minitest::Mock works; the mock object is a wrapper around the "real" instance of A , and defined expectations intercept the...

How to set server time in Node.js

How to set server time in Node.js I would like to set (mock) my custom time to the Node.js server for testing purposes. Something like this: console.log(new Date()); // Sat Jun 30 2018 20:00:00 GMT+0100 // ^^ someFunctionToSetTime('Sat Jun 30 2018 12:00:00 GMT+0100') // ^^ console.log(new Date()); // Sat Jun 30 2018 12:00:00 GMT+0100 // ^^ How do I do this? P.S. I don't really want to fake the Date class. Date Sinon's fake timers are nice for this: sinonjs.org/releases/v6.0.1/fake-timers – Mark_M Jun 30 at 20:01 Possible duplicate of stackoverflow.com/questions/43089562/…? – prototype Jun 30 at 20:04 ...