Possible to stub method twice within a single test to return different results?

Sinon has a nice API for handling multiple calls (stub.onCall(n);) to the same stubbed method. Example from stub api doc: “test should stub method differently on consecutive calls”: function () { var callback = sinon.stub(); callback.onCall(0).returns(1); callback.onCall(1).returns(2); callback.returns(3); callback(); // Returns 1 callback(); // Returns 2 callback(); // All following calls return 3 } You …

Read more

What is double method in rspec for?

Edit: I just reread your question and realized I didn’t quite answer it. Leaving my original answer because it’s related, but here’s your specific answer: The reason you don’t need a double is because you’re stubbing class methods, rather than instance methods. double is only useful for dealing with instances of the class, not the …

Read more

Testing software: fake vs stub

I assume you are referring to the terminology as introduced by Meszaros. Martin Fowler does also mentions them regularly. I think he explains the difference pretty well in that article. Nevertheless, I’ll try again in my own words 🙂 A Fake is closer to a real-world implementation than a stub. Stubs contain basically hard-coded responses …

Read more

How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.?

Type stubs are sometimes packaged directly with the library. Otherwise there can be some external libraries to provide them. Numpy Starting with numpy 1.20 type stubs will be included in numpy. See this changelog and this PR adding them Before that they could added with the library https://github.com/numpy/numpy-stubs Pandas and Matplotlib There is no official …

Read more

How do I stub new Date() using sinon?

I suspect you want the useFakeTimers function: var now = new Date(); var clock = sinon.useFakeTimers(now.getTime()); //assertions clock.restore(); This is plain JS. A working TypeScript/JavaScript example: var now = new Date(); beforeEach(() => { sandbox = sinon.sandbox.create(); clock = sinon.useFakeTimers(now.getTime()); }); afterEach(() => { sandbox.restore(); clock.restore(); });

How do I stub things in MiniTest?

# Create a mock object book = MiniTest::Mock.new # Set the mock to expect :title, return “War and Piece” # (note that unless we call book.verify, minitest will # not check that :title was called) book.expect :title, “War and Piece” # Stub Book.new to return the mock object # (only within the scope of the …

Read more