简体   繁体   English

在yeoman angularjs中使用模拟文件夹进行业力测试

[英]using the mock folder for karma test in yeoman angularjs

I have a angularjs application, which I generated with yeoman. 我有一个angularjs应用程序,我用自己生成的。 In the karma.conf.js is a reference to test/mock/**/*.js. 在karma.conf.js中是对test / mock / ** / * .js的引用。 I have troubles to find out, how I use this folder. 我有麻烦找出来,我如何使用这个文件夹。 Currently I have a simple Service: 目前我有一个简单的服务:

'use strict';

angular.module('tvcalApp')
  .factory('Series', function ($resource) {
    return $resource('/search/:search');
  });

and a Test 和测试

'use strict';

var $httpBackend;

describe('Service: Series', function () {

  // load the service's module
  beforeEach(module('tvcalApp'));

  // instantiate service
  var Series;
  beforeEach(inject(function (_Series_) {
    Series = _Series_;
  }));

  beforeEach(inject(function ($injector) {
      var url_get = '/search/The%20Simpsons';

      var response_get = [{"seriesid": "71663"}];

      $httpBackend = $injector.get('$httpBackend');

      $httpBackend.whenGET(url_get).respond(response_get);

  }));

  it('should return a list if search for The Simpsons', function () {
      var res = Series.query({search: 'The Simpsons'});
      $httpBackend.flush();
        expect(res[0].seriesid === 71663);
      });

});

This is working. 这很有效。 But I wonder If I could use the mock folder from the karma.conf.js for the mocking function. 但我想知道如果我可以使用karma.conf.js中的mock文件夹作为模拟函数。 Is it possible to move the mock part into the mock folder and use it for all unit test? 是否可以将模拟部分移动到模拟文件夹中并将其用于所有单元测试?

I could not find any example or documentation for this folder. 我找不到此文件夹的任何示例或文档。 Can someone please point me to to an example or documentation how to use the mock folder. 有人可以指点我的示例或文档如何使用模拟文件夹。

Basically i have done something like this looking at angular-mocks.js: 基本上我做了类似的事情看了angular-mocks.js:
Let's say may app is called ql. 假设app可以称为ql。 and i have a loginService that i want to mock: 我有一个我想模拟的loginService:

mocks/servicesMock.js looks like this: mocks / servicesMock.js看起来像这样:

'use strict';

var ql = {};
ql.mock = {};

ql.mock.$loginServiceMockProvider = function() {
  this.$get = function() {
    var $service = {
      login: function() { }
    };
    return $service;
  };
};

angular.module('qlMock', ['ng']).provider({
  $loginServiceMock: ql.mock.$loginServiceMockProvider
});

Then in my tests i can injeck $loginServiceMock: 然后在我的测试中,我可以injeck $ loginServiceMock:

'use strict';

describe('LoginController tests', function () {
  // load the controller's module
  beforeEach(module('ql'));
  // load our mocks module
  beforeEach(angular.mock.module('qlMock'));

  var loginController,
    loginServiceMock,
    scope;

  // Initialize the controller and a mock scope
  // $loginSericeMock will be injected from serviceMocks.js file
  beforeEach(inject(function ($controller, $rootScope, $loginServiceMock) {
    scope = $rootScope.$new();
    loginServiceMock =  $loginServiceMock;
    loginController = $controller('LoginController', {
      $scope: scope,
      loginService: loginServiceMock
    });
  }));
});

The example by @gerasalus is useful, but to answer the question: @gerasalus的例子很有用,但要回答这个问题:

mocks is just a folder to put your code in to keep your project organized and the code in tests short and to the point. mocks只是一个文件夹,用于放置代码以保持项目的有序性,并且测试中的代码简短而重要。 By keeping all your mocks in one place, it is easier to reuse them in tests... copying them from one test to another would be bad practice from a DRY perspective. 通过将所有模拟保存在一个地方,在测试中重用它们更容易......从DRY的角度来看,将它们从一个测试复制到另一个测试将是不好的做法。

So, for example, you might have a service called 'Foo' 因此,例如,您可能有一个名为'Foo'的服务

app/service/foo.js

Then you might create a mock of that service, called 'FooMock' 然后你可以创建一个名为'FooMock'的服务的模拟

test/mocks/service/foo.js

And then you would create tests and inject whatever mocks you need, as is shown in gerasulus's answer. 然后你会创建测试并注入你需要的任何模拟,如gerasulus的回答所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM