Angularjs Jasmine Failure-TypeError:字符串不是函数
我一直在尝试为一个小JavaScript文件编写Jasmine测试。此文件为我的应用程序定义了一个名为“EIS”的事件。在我的Jasmine测试运行之前,我在Jasmine Specs Runner中收到一条失败消息,因为它无法正确读取我的过滤器。我遵循了这个问题中的简单说明: 但这没有帮助,因为Jasmine甚至不会读取我原始javascript文件中的过滤器。这是(标题为eventModule.js): 以下是Jasmine故障描述:Angularjs Jasmine Failure-TypeError:字符串不是函数,angularjs,jasmine,angularjs-filter,Angularjs,Jasmine,Angularjs Filter,我一直在尝试为一个小JavaScript文件编写Jasmine测试。此文件为我的应用程序定义了一个名为“EIS”的事件。在我的Jasmine测试运行之前,我在Jasmine Specs Runner中收到一条失败消息,因为它无法正确读取我的过滤器。我遵循了这个问题中的简单说明: 但这没有帮助,因为Jasmine甚至不会读取我原始javascript文件中的过滤器。这是(标题为eventModule.js): 以下是Jasmine故障描述: TypeError: string is not a
TypeError: string is not a function
at new EISEvent (http://localhost:8080/EISViewer/js/eventModule.js:21:41)
我没有包括所有的js代码,但是第21行是一个有**的代码。当应用程序控制器在另一个javascript文件中调用我的过滤器时,它就会工作。所以,也许我需要在Jasmine测试中包含控制器和“EISEvent”函数调用?这是我的茉莉花测试,如果有帮助的话:
describe( "EISEvent", function(){
var eisEvent;
var fakeDate = '10/23/2012';
var fakeFilter;
//inject the filter to resolve the dependency
beforeEach(function() {
inject(function ($injector) {
//mock the services here
fakeFilter = $injector.get('$filter')('date');
})
});
beforeEach( function(){
eisEvent = new EISEvent( "L", "theName", "theSource", "<beginTag>somexml</beginTag>", fakeFilter);
});
it( "Should be initialized", function(){
expect( eisEvent ).toBeDefined();
});
});
描述(“EISEvent”,函数(){
事件;
var fakeDate='10/23/2012';
var假滤波器;
//注入过滤器以解决依赖关系
beforeach(函数(){
注入(函数($injector){
//在这里模拟服务
fakeFilter=$injector.get('$filter')('date');
})
});
beforeach(函数(){
eisEvent=新的eisEvent(“L”、“theName”、“theSource”、“somexml”、fakeFilter);
});
它(“应该初始化”,函数(){
expect(eisfevent.toBeDefined();
});
});
您的EISEvent
需要Angular的$filter
服务实例。但是在测试中,您通过的是实际的日期过滤器函数(不是$filter
服务的实例)
因此,在测试中修改beforeach()的beforeach()
:
beforeEach(function() {
inject(function ($injector) {
//mock the services here
fakeFilter = $injector.get('$filter');
})
});
说得迂腐一点,它并不像您命名的变量那样是一个真正的“假过滤器”。执行以下操作时,您正在检索实际的$filter
服务:$injector.get(“$filter”)代码>
当您将$filter
服务传递给EISEvent
类时,测试中的代码应该与应用程序中的代码一样工作
为了弄清楚eisfevent
中的代码在做什么,让我们将其分解
原始代码:
this.timestamp = $filter('date')(date, "yyyy-MM-dd HH:mm:ss a");
在上面,$filter('date')
检索Angular的“date”过滤器函数。然后(date,“yyyy-MM-dd HH:MM:ss a”)
执行过滤函数,传递date
变量和日期格式字符串。成功了,谢谢!我也很感激你的解释,因为现在我明白了。我不知道传递给测试过滤器变量的是什么
this.timestamp = $filter('date')(date, "yyyy-MM-dd HH:mm:ss a");