I'm currently struggling with the .then() function while trying to implement Jasmine unit testing. Here is the code that's giving me trouble:
describe("getBuilding", function () {
it("checks getBuilding", function () {
var id_building = 4;
LocalDB.getTestData();
LocalDB.getBuilding(id_building).then(function (result) {
expect(result.name).toMatch("Something");
});
});
});
In this scenario, the result variable seems to have the correct value in the then() function, but the expect statement doesn't seem to work properly. Even if I change "Something" to "something else," the test still passes when it shouldn't.
I attempted to resolve the issue by modifying the code like so:
describe("getBuilding", function () {
it("checks getBuilding", function () {
var id_building = 4;
LocalDB.getTestData();
expect(LocalDB.getBuilding(id_building).name).toMatch("Something");
});
});
or
describe("getBuilding", function () {
it("checks getBuilding", function () {
var finalResult;
var id_building = 4;
LocalDB.getTestData();
LocalDB.getBuilding(id_building).then(function (result) {
finalResult=result.name;
});
expect(finalResult).toMatch("Something");
});
});
However, in both cases, the value being matched turns out to be undefined. Would appreciate any advice on how to tackle this issue. Thank you!