Stubbing window.location.href with Sinon
You need to use global to mock the window object for your test in beforeEach or it e.g. it(‘should compose a Log’, () => { global.window = { location: { href: { value: ‘foo’ } } } //…. call the funciton });
You need to use global to mock the window object for your test in beforeEach or it e.g. it(‘should compose a Log’, () => { global.window = { location: { href: { value: ‘foo’ } } } //…. call the funciton });
You can provide an implementation for a method with the Do() handler: Func<TypeX,TypeY,TypeZ,TypeZ> returnThird = (x,y,z) => z; mock.Expect(x => x.Method(null, null, null)).IgnoreArguments().Do(returnThird); Note that TypeZ appears twice because it is both an input argument type and the return type.
Note you cannot directly call M.foo! Your code only seems to work because you mocked M.foo to return :bar. When you open A metaclass (class << self) to include M, you have to mock any instance of M, that is adding to your before block: allow_any_instance_of(M).to receive(:foo).and_return(:bar) module M def foo :M end end module … Read more
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
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
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
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.
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