如何提供可重复使用的样本数据值以我angularjs /茉莉单元测试 [英] How do I provide re-usable sample data values to my angularjs / jasmine unit-tests

查看:111
本文介绍了如何提供可重复使用的样本数据值以我angularjs /茉莉单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供简单的恒定值,如姓名,电子邮件地址,等在我的茉莉花单元测试使用。

I would like to provide simple constant values such as names, emails, etc to use in my jasmine unit tests.

有一个类似的问题在这里问: AngularJS +噶:重用一个模拟服务时,单元测试指令或控制器

A similar question was asked here: AngularJS + Karma: reuse a mock service when unit testing directives or controllers

在C#我想创建一个静态类来保存数据模拟小片段。然后我就可以使用这些值在我的单元测试,像这样的:

In c# I would create a static class to hold little snippets of mock data. I can then use these values throughout my unit tests, like this:

static class SampleData
{
    public const string Guid = "0e3ae555-9fc7-4b89-9ea4-a8b63097c50a";
    public const string Password = "3Qr}b7_N$yZ6";

    public const string InvalidGuid = "[invalid-guid]";
    public const string InvalidPassword = "[invalid-password]"; 
}

我想有一样方便使用测试噶/茉莉我AngularJS应用程序的时候。

I would like to have the same convenience when testing my AngularJS app using Karma / Jasmine.

我知道我可以定义对我的角度应用一个对象,我已经为常量我在现实code使用,这样做:

I know that I can define a constant object against my angular app, I already do this for constants I use in the real code, like this:

myApp.constant('config', {apiUrl:'http://localhost:8082'})

我可以添加另一个常量就是这样的,但只有包含样本数据值用在我的单元测试,像这样的:

I could add another constant just like this but only containing sample data values for use in my unit tests, like this:

myApp.constant('sampleData', {email: 'sample@email.com'}) 

然后我可以只注入模拟常量对象为我的测试和关闭我去,这样

I could then just inject the mock constant object into my tests and off I go, like this

describe 'A sample unit test', ->
    beforeEach ->   module 'myApp'
    beforeEach inject ($injector) ->
        @sampleData = $injector.get 'sampleData'
        email = @sampleData.email
        # etc ...

然而,这似乎有点腥给我。我不希望我的生产code以包含只有我的单元测试所需的样本数据。

However this seems a bit fishy to me. I don't want my production code to contain sample data that is only required by my unit-tests.

您将如何方便地提供您的角/茉莉单元测试可重复使用的样本数据值?

How would you conveniently provide your angular / jasmine unit tests with re-usable sample data values?

感谢

推荐答案

有这样做的两种方式:


  • 窥视函数调用和返回假值。

  • 创建模拟类(也可能是模拟数据初始化它们)并加载它们,无论你需要

第一个是正常的,当你只需要假打了几个电话。这样做了整整类是不可持续的。

The first one is alright when you only have to fake a few calls. doing that for a whole class is unsustainable.

例如,假设你有建立一些特殊的URL的服务。如果其中的一种方法取决于absUrl,可以通过刺探在 $地址的一种方法假货对象:

For example, let's say you have a service that builds some special URLs. If one of the methods depends on absUrl, you can fake it by spying on the method in the $location object:

describe('example') function () {
    beforeEach(inject(function () {
        spyOn($location, 'absUrl').andCallFake(function (p) {
            return 'http://localhost/app/index.html#/chickenurl';
        });
    }));
it('should return the url http://www.chicken.org') ... {
    // starting situation
    // run the function
    // asserts
}

现在让我们假设你有一个设置服务,封装如语言,主题,特殊路径,背景颜色,字体数据......这是使用远程调用初始化到服务器。
测试依赖于设置服务将是痛苦的。你每次都嘲笑与spyOn一个很大的组成部分。如果你有10个服务......你不想copypaste在所有这些的spyOn功能。

Now let's say that you have a Settings Service that encapsulates data like language, theme, special paths, background color, typeface... that is initialised using a remote call to a server. Testing services that depend on Settings will be painful. You have to mock a big component with spyOn every time. If you have 10 services... you don't want to copypaste the spyOn functions in all of them.

ServiceA 使用设置服务:

describe('ServiceA', function () {
var settings, serviceA;

beforeEach(module('myapp.mocks.settings'));  // mock settings
beforeEach(module('myapp.services.serviceA')); // load the service being tested

beforeEach(inject(function (_serviceA_, _settings_) {
    serviceA = _serviceA_;
    settings = _settings_;
}));

容器
这个测试套件,到设置的所有呼叫服务将被模拟,它具有相同的接口真实的,但返回虚拟值处理。
请注意,您可以在任何地方加载此服务。

container for this test suite, all calls to the Settings service will be handled by the mock, which has the same interface as the real one, but returns dummy values. Notice that you can load this service anywhere.

(如果以任何理由,你需​​要使用真正实施,可以装载真正实施前的模拟,并使用spyOn针对特定的情况下委托调用真正落实。)

(If, by any reason, you needed to use the real implementation, you can load the real implementation before the mock and use spyOn for that particular case to delegate the call to the real implementation.)

通常你放置嘲笑模块的应用程序文件夹之外。我有一个单元测试,端到端测试,并与角mocks.js文件中的 LIB 文件夹测试文件夹。我把我的嘲笑也有。
告诉你的业力需要测试的文件:

Normally you'll place the mocks module outside of the app folder. I have a test folder with the unit tests, e2e tests and a lib folder with the angular-mocks.js file. I place my mocks there too. Tell karma the files you need for the tests:

    files: [
      'app/lib/jquery/jquery-1.9.1.js',
      'test/lib/jasmine-jquery.js',
      'app/lib/angular/angular.js',
      'app/lib/angular/angular-*.js',
      'test/lib/angular/angular-mocks.js',
      'test/lib/myapp/*.js', /* this is mine */
      'app/js/**/*.js',
      'test/unit/**/*.js'
    ],

文件测试/ lib中/ myapp.mocks.settings.js 看起来就像任何其他的模块:

The file tests/lib/myapp.mocks.settings.js looks just like any other module:

(function () {
"use strict";

var m = angular.module('myapp.mocks.settings', []);

m.service('settings', function () { ... })
})

问题二(可选):要快速更改虚拟值。在这个例子中,设置服务获取从当它被实例化的第一次在服务器中的对象。然后,该服务具有用于所有领域的吸气剂。这是怎样的一个代理服务器:不是送你需要一个值的请求每次,取一群人并保存在本地。在我的应用程序,设置不会在运行时服务器发生改变。

Second problem (optional): you want to change quickly the dummy values. In the example, the settings service fetches an object from the server when it is instantiated for the first time. then, the service has getters for all fields. This is kind of a proxy to the server: instead of sending a request everytime you need a value, fetch a bunch of them and save them locally. In my application, settings don't change in the server in run-time.

是这样的:

1. fetch http://example.org/api/settings
2. var localSettings = fetchedSettings;
3  getFieldA: function() { return localSettings.fieldA; }

你去污染全局命名空间。我创建了一个文件 settings.data.js 与内容,如:

var SETTINGS_RESPONSE = { fieldA: 42 };

模拟服务使用这个全局变量在工厂实例 localSettings

这篇关于如何提供可重复使用的样本数据值以我angularjs /茉莉单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆