- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为服务运行 jasmine 单元测试。我模拟了 $location 但出现错误:
app.factory('testService', function($location) {
return {
config: function() {
var host = $location.absUrl();
var result = '';
if (host.indexOf('localhost') >= 0) {
return 'localhost'
}
if (host.indexOf('myserver') >= 0) {
return 'myserver'
}
}
};
});
我的测试是这样的:
describe('testing service', function () {
var configService, $rootScope, $location;
beforeEach(module('myApp'));
beforeEach(inject(function (_$location_) {
//$rootScope = _$rootScope_;
$location = _$location_;
//configService=_configService_;
spyOn($location, 'absUrl').andReturn('localhost');
}));
it('should return something ', function () {
inject(function (configService) {
//expect($location.absUrl).toHaveBeenCalled();
//expect($rootScope.absUrl()).toBe('localhost');
var result= configService.config($location);
expect(result).toBe('localhost');
});
});
});
这是我得到的错误: TypeError: spyOn(...).andReturn 不是函数?
最佳答案
Jasmine 2.0 改变了一些 spy 语法。查看Jasmine 2.0 docs .
请使用:
spyOn($location, 'absUrl').and.returnValue('localhost');
关于javascript - 如何解决TypeError : spyOn andReturn is not a function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32599346/
当我尝试监视 $scope.$watch 的监听器函数时,就像从未调用过 spy On http://jsfiddle.net/b8LoLwLb/1/ 我的 Controller angular.mo
我正在尝试为 Angular 应用程序编写单元测试测试用例,并且我正在使用 SpyOn() 方法来监视服务方法。 我正在测试一个服务,它有一个名为 getCurrentBoardTimeIdByCur
我确实有一个具有 2 个重载方法的类。 public static create( this: ModelStatic, values?: M['_creationAttribut
我有一个函数,我只想在第二次调用和第三次调用时模拟,但在第一次调用时使用默认实现。我查看了 Jest 文档,并且有一个函数 mockImplementationOnce 可以用来模拟单个调用的实现。
案例 当我在 rootScope 上创建一个 spy 时,期望由于某种原因失败了。查看 plunkr 并尝试将其注释掉并反向查看。 代码 Plunker Example describe('Testi
我有一个 Angular Controller ,其方法调用 $location.search() 两次。 第一次只是$location.search()返回值。 第二次是 $location.sea
为什么 jest.spyOn 不能使用在测试现场解构的解构函数? 以下测试将失败: export const Funcs = { foo: () => { return 'foo';
我正在使用 Jest 来测试我的 React 组件,但我遇到了一个我以前从未见过的错误。 这是我的 组件: class Row extends React.Component { construc
如果在回调函数中调用该方法,则在对象上使用 spyOn 似乎会失败。 jasmine 不会注意到回调中对方法的任何调用。 请看下面的代码,我在 child.print() 方法上创建了一个 spy 。
对于下面的代码: class Endpoint { constructor(type, start_value) { this.type = type this.end_value
我正在尝试在 React Native 应用程序中测试异步函数。 class myClass extends React.Component { ... closeModal = async
我试图用 Jasmine 测试的函数获取一个对象数组,然后使用 splice() 方法根据函数传递的参数对其重新排序。 我知道我使用 spyOn().and.returnValue() 伪造返回数组。
我正在测试调用其辅助函数 callApi 的 apiMiddleware。为了防止调用将发出 API 调用的实际 callApi,我模拟了该函数。但是,它仍然会被调用。 apiMiddleware.j
我正在尝试为一个简单的 React 组件编写一个简单的测试,并且我想使用 Jest 来确认在我使用 enzyme 模拟点击时调用了一个函数。根据 Jest 文档,我应该能够使用 spyOn 来执行此操
我将 Jasmine-Species 与 jasmine 一起用于 GWT 。 我编写了一个如下所示的测试 feature('checking spy', function() { summary(
您好,我有一个关于使用 Jasmine 模拟 JS 代码的问题。 想象一下有以下情况: function Test(){ var a = 5; var b = 3; Test2
我遇到了一个问题,试图监视在构造函数中调用的服务函数调用。测试是基本的,只是验证函数调用是否实际被调用。 beforeEach(async(() => { TestBed.configure
我定义了一个接口(interface)和不透明的token如下 export let AUTH_SERVICE = new OpaqueToken('auth.service'); export in
我正在使用 Karma-Jasmine 为我的组件(Angular2 应用程序)编写单元测试。我正在使用 Istanbul 进行代码覆盖率报告。 这是我的测试用例, it('Should Invoke
我想用 Jasmine 测试我的 Angular 应用程序。所以我创建了一些测试,其中大部分都运行良好。但是,我的功能之一要求用户填写提示。测试无法填充此提示,所以我用 spyOn(window,'p
我是一名优秀的程序员,十分优秀!