【问题标题】:Is there way to spyOnProperty window.location.search using jasmine unit testing? because search is not configurable有没有办法使用 jasmine 单元测试来 spyOnProperty window.location.search?因为搜索是不可配置的
【发布时间】:2020-09-01 07:24:48
【问题描述】:
抛出错误:搜索不可配置
it('should have search keyword', function () {
spyOnProperty(window.location, 'search').and.returnValue("?q=jag");
expect(window.location.search).toBe("?q=jag");
});
【问题讨论】:
标签:
javascript
angularjs
unit-testing
jasmine
karma-jasmine
【解决方案1】:
beforeEach(function () {
window.history.pushState({}, '', "?q=jag");
})
it('should have search keyword', function () {
expect(window.location.search).toBe("?q=jag");
expect($('#decoder-search-bar-input').val()).toEqual('jag');
});