javascript angularjs 中的全局模拟对象,用于茉莉花/业力测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22869822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Globally mock object in angularjs for jasmine/karma testing
提问by Deslyxia
I have an object that I am mocking up for unit testing. Essentially in my test file I mock it up as follows:
我有一个用于单元测试的对象。基本上在我的测试文件中,我将其模拟如下:
var mockObject = {
mockMethod1 : function() {return true},
mockMethod2 : function() {return true}
};
beforeEach(module('myModule') , function ($provide) {
$provide.value('realObject',mockObject);
});
The way i understand it is that as I test functionality in my module etc... anywhere that references the "realObject" will use my "mockObject"
我的理解是,当我在我的模块等中测试功能时......任何引用“realObject”的地方都将使用我的“mockObject”
My issue is that I have made multiple js files for testing and I do not want to define my "mockObject" in each one of them ... nor do i want to maintain it in any more places than i have too.
我的问题是我已经制作了多个 js 文件进行测试,我不想在每个文件中定义我的“mockObject”……我也不想在比我多的地方维护它。
Is there a way to move my "mockObjact" to a seperate file that gets included in karma.conf.js that will make the "mockObject" available for injection into any of my test files ..... Im thinking along the lines of how you inject $rootScope
有没有办法将我的“mockObjact”移动到一个单独的文件中,该文件包含在 karma.conf.js 中,这将使“mockObject”可用于注入我的任何测试文件中......你如何注入 $rootScope
回答by Eitan Peer
You can create a global beforeEach function if it is written outside the context of a specific suite, but still executed by Jasmine, e.g. create a custom file to load by Karma and write your beforeEach function there without enclosing it in a describe function.
如果它是在特定套件的上下文之外编写的,但仍由 Jasmine 执行,您可以创建一个全局 beforeEach 函数,例如,创建一个自定义文件以供 Karma 加载,并在那里编写您的 beforeEach 函数,而无需将其包含在描述函数中。
Example:
例子:
var myGlobal;
beforeEach(function() {
// This will run before any it function.
// Resetting a global state so the change in this function is testable
myGlobal = 10
});
describe('first suite', function(){
it('is a test', function(){
expect(myGlobal).toBe(10);
// Set the value to show that beforeEach is executed for each it function
myGlobal = 20;
expect(myGlobal).toBe(20);
});
it('is another test', function(){
expect(myGlobal).toBe(10);
myGlobal = 30;
expect(myGlobal).toBe(30);
});
});
describe('second suite', function(){
it('is a test', function(){
expect(myGlobal).toBe(10);
});
});
See fiddle here
在这里看到小提琴
回答by aet
You could build a service that houses your mock, and inject that service in each test file.
您可以构建一个包含您的模拟的服务,并在每个测试文件中注入该服务。
beforeEach(inject(function($rootScope, $controller, yourMockSvc) {
appScope = $rootScope.$new();
appCtrl = $controller('AppController', {
$scope: appScope,
yourSvc: yourMockSvc
});
}));
回答by Archmede
Just define your mock object inside of another file and export it. You should be able to use it in any file.
只需在另一个文件中定义您的模拟对象并将其导出。您应该能够在任何文件中使用它。