Open woprzech opened 6 years ago
I started looking into that. It looks like the error is in our code that prepares response from sinon
Example, I'm waiting for response with body that is simple json with attribute message. I've modified the "method" method in server.ts to use sinon like so:
server.respondWith("POST", url, [200, {"Content-Type": "application/json"}, '{"message": "Goodbye Jane!"}']);
And the tests works fine. I think that we have problem with using server.respondWith.
In following example that uses only sinon and Angular (without ng-test-runner
) first test (sinon respondWith with function
) fails, but second (sinon respondWith with body
) works fine.
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { TestBed } from "@angular/core/testing";
import * as sinon from "sinon";
import { SinonFakeServer, SinonFakeXMLHttpRequest } from "sinon";
describe("Sinon with Angular Async", () => {
let server: SinonFakeServer;
let http: HttpClient;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule
]
});
http = TestBed.get(HttpClient);
server = sinon.fakeServer.create();
server.respondImmediately = false;
server.autoRespond = false;
});
afterEach(() => {
server.restore();
});
it("sinon respondWith with function", (done: DoneFn) => {
server.respondWith("GET", /\/test/, (req: SinonFakeXMLHttpRequest) => {
req.respond(200, {"Content-Type": "application/json"}, JSON.stringify({message: "Text"}));
});
http.get("/test").subscribe(
(resp) => {
console.log(`Got response ${JSON.stringify(resp)}`);
expect(resp).toEqual({message: "Text"});
done();
}
);
server.respond();
});
it("sinon respondWith with body", (done: DoneFn) => {
server.respondWith("GET", /\/test/, JSON.stringify({message: "Text"}));
http.get("/test").subscribe(
(resp) => {
console.log(`Got response ${JSON.stringify(resp)}`);
expect(resp).toEqual({message: "Text"});
done();
}
);
server.respond();
});
});
I am unable to run test with
respondImmediately : false
server configuration - even for simple test case from documentation.From test from documentation
I'm getting an error: