0
私はジャスミンを初めて使っています。以下のコードでジャスミン単位テストケースを書く必要があります。jasmineでメソッド呼び出しを含む単純な条件をテストする方法
if(checkForRelationship()){
var relationship = localStorage.getItem('relationship');
}
else{
localStorage.setItem('relationship',{id:'someId'});
}
// where checkForRelationship method is like below
function checkForRelationship(){
return !!localStorage.getItem('relationship');
}
あなたの助けに感謝。あなたは、少なくとも始めるべきです
describe("Relationship in local storage", function() {
beforeEach(function() {
spyOn(localStorage, "getItem");
spyOn(localStorage, "setItem");
});
it("should set relationship in localStorage if it doesn't exist", function() {
localStorage.getItem.and.returnValue = null;
run() // <- Here you put whatever you need to make your code run
expect(localStorage.setItem).toHaveBeenCalledWith("relationship", { id: "someId" });
});
it("should not set relationship in localStorage if it already exists", function() {
localStorage.getItem.and.returnValue = "truthy";
run() // <- Here you put whatever you need to make your code run
expect(localStorage.setItem).not.toHaveBeenCalled();
});
});
:
ありがとうミカエル! –