我有一个登录过程,在向服务器发送请求并获得响应后,我会执行以下操作:
this.$auth.setToken(response.data.token);
this.$store.dispatch("setLoggedUser", {
username: this.form.username
});
现在我想在使用cypress进行测试时模拟这种行为,所以我不需要每次运行测试时都实际登录.
所以我创建了一个命令:
Cypress.Commands.add("login", () => {
cy
.request({
method: "POST",
url: "http://localhost:8081/api/v1/login",
body: {},
headers: {
Authorization: "Basic " + btoa("administrator:12345678")
}
})
.then(resp => {
window.localStorage.setItem("aq-username", "administrator");
});
});
但我不知道如何模仿"setLoggedUser"的动作,知道吗?