How to stub a Typescript-Interface / Type-definition?

I have been writing Typescript tests using qUnit and Sinon, and I have experienced exactly the same pain you are describing. Let’s assume you have a dependency on an interface like: interface IDependency { a(): void; b(): boolean; } I have managed to avoid the need of additional tools/libraries by using a couple of approaches … Read more

RSpec: how to test Rails logger message expectations?

While I agree you generally don’t want to test loggers, there are times it may be useful. I have had success with expectations on Rails.logger. Using RSpec’s deprecated should syntax: Rails.logger.should_receive(:info).with(“some message”) Using RSpec’s newer expect syntax: expect(Rails.logger).to receive(:info).with(“some message”) Note: In controller and model specs, you have to put this line before the message … Read more

Stubbing authentication in request spec

A request spec is a thin wrapper around ActionDispatch::IntegrationTest, which doesn’t work like controller specs (which wrap ActionController::TestCase). Even though there is a session method available, I don’t think it is supported (i.e. it’s probably there because a module that gets included for other utilities also includes that method). I’d recommend logging in by posting … Read more

Mockito – NullpointerException when stubbing Method

I had this issue and my problem was that I was calling my method with any() instead of anyInt(). So I had: doAnswer(…).with(myMockObject).thisFuncTakesAnInt(any()) and I had to change it to: doAnswer(…).with(myMockObject).thisFuncTakesAnInt(anyInt()) I have no idea why that produced a NullPointerException. Maybe this will help the next poor soul.

Cleaning up sinon stubs easily

Sinon provides this functionality through the use of Sandboxes, which can be used a couple ways: // manually create and restore the sandbox var sandbox; beforeEach(function () { sandbox = sinon.sandbox.create(); }); afterEach(function () { sandbox.restore(); }); it(‘should restore all mocks stubs and spies between tests’, function() { sandbox.stub(some, ‘method’); // note the use of … Read more

tech