问题
In my file, I have something like this:
if(somevar.toString().length == 2) ....
How can I spy on toString
from my test file? I know how to spy on things like parseInt with:
let spy = sinon.spy(global, 'parseInt')
But that doesn't work with toString
since it's called on the value, I tried spying on Object
and Object.prototype
, but that doesn't work either.
回答1:
You can't call sinon.spy
or sinon.stub
on a method of primitive value like:
sinon.spy(1, 'toString')
. This is wrong.
You should call them on the Class.prototype
of primitive value. Here is the unit test solution:
index.ts
:
export function main(somevar: number) {
if (somevar.toString(2).length == 2) {
console.log("haha");
}
}
index.spec.ts
:
import { main } from "./";
import sinon from "sinon";
import { expect } from "chai";
describe("49866123", () => {
afterEach(() => {
sinon.restore();
});
it("should log 'haha'", () => {
const a = 1;
const logSpy = sinon.spy(console, "log");
const toStringSpy = sinon.stub(Number.prototype, "toString").returns("aa");
main(a);
expect(toStringSpy.calledWith(2)).to.be.true;
expect(logSpy.calledWith("haha")).to.be.true;
});
it("should do nothing", () => {
const a = 1;
const logSpy = sinon.spy(console, "log");
const toStringSpy = sinon.stub(Number.prototype, "toString").returns("a");
main(a);
expect(toStringSpy.calledWith(2)).to.be.true;
expect(logSpy.notCalled).to.be.true;
});
});
Unit test result with 100% coverage:
49866123
haha
✓ should log 'haha'
✓ should do nothing
2 passing (28ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.spec.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/49866123
来源:https://stackoverflow.com/questions/49866123/sinon-spy-on-tostring-method