【问题标题】:Jasmine test with different matches in jsonjson中不同匹配的茉莉花测试
【发布时间】:2017-07-05 20:14:00
【问题描述】:

我想用下面的代码实现 jasmine 测试:

这是我的 filter-service.js

 function FilterService() { 
 }

 FilterService.prototype.filter = function (companies, filter) {
   return companies;
 };

而我的 filter-service-spec.js 是

    describe("Filter service filter(...) tests", function() {
var filterService = new FilterService();
var allTestCompanies= [
    { id: 1, name: "company1 Test", admin: "Test Admin" },
    { id: 2, name: "company2", admin: "Test Admin", country: 'London' },
    { id: 3, name: "company3", admin: "Mery Phill" }      
];        

it('returns original collection if called with undefined filter', function() {
    var input = [1, 2, 3];

    var result = filterService.filter(input, undefined);

    expect(result).toEqual(input);
});
it('returns original collection if called with empty filter', function () {
    var input = [2, 6, 7];

    var result = filterService.filter(input, '');

    expect(result).toEqual(input);
});

it('only includes matching companies once', function() {
    var result = filterService.filter(allTestCompanies, 'Test');

    expect(result.length).toEqual(2);
});

it('matches exact text on company name', function() {
    var result = filterService.filter(allTestCompanies, "company1 Test");

    expect(result[0]).toEqual(allTestCompanies[0]);
});
it('matches exact text contained in company name', function () {
    var result = filterService.filter(allTestCompanies, "Test");

    expect(result[0]).toEqual(allTestCompanies[0]);
});
it('matches case invarient text contained in company name', function () {
    var result = filterService.filter(allTestCompanies, "test");

    expect(result[0]).toEqual(allTestCompanies[0]);
});

it('matches exact text of admin', function() {
    var result = filterService.filter(allTestCompanies, 'Mery Phill');

    expect(result[0]).toEqual(allTestCompanies[2]);
});
it('matches exact text in admin', function () {
    var result = filterService.filter(allTestCompanies, 'Phil');

    expect(result[0]).toEqual(allTestCompanies[2]);
});
it('matches case invarient text in admin', function () {
    var result = filterService.filter(allTestCompanies, 'PHIl');

    expect(result[0]).toEqual(allTestCompanies[2]);
});   
});

我如何在 filter-service.js 中实现一个函数来通过 javascript 测试。现在只能先通过 2。

【问题讨论】:

  • 您的代码除了返回传入的第一个参数之外什么都不做。因此,如果您针对第一个参数测试结果,它们当然会通过。你没有告诉我们你想做什么,它就像你希望有人为你写代码一样
  • 你请别人为你写代码吗?

标签: javascript angularjs unit-testing jasmine


【解决方案1】:

当然,这实际上很容易做到,但我将提供您应该采取的方法。那么让我们开始吧……

  1. 我希望您有一个可以运行测试的环境(安装了 Jasmine 并且您可以针对“FilterService”对象和“filter”方法运行测试)。你说前两种方法都成功了。
  2. 每个 Jasmine “it”函数都有描述,例如“匹配公司名称的确切文本”(测试 #4)。这是您将要处理的最重要的部分。我们将讨论这个测试。问问自己这个描述告诉你什么?这意味着如果您将公司的确切名称传递给“过滤器”方法,该函数应该遍历给定公司对象的数组并尝试找到与确切公司名称匹配的对象。
  3. 接下来,您需要查看 Jasmine “expect”函数的实现。您会注意到“过滤器”函数调用返回的结果必须等于给定公司数组的第一个元素,因为过滤器参数中的公司名称与该对象的“名称”匹配。
  4. 实现这部分并测试成功后,您将进入下一个测试并添加/更改现有实现以完成下一个“it”描述。

这称为 TDD(测试驱动开发)。在开始工作之前,请先阅读一下。

最后让我们尝试实现这个测试#4。请注意,提供的代码可能无法正常工作,但正如我们同意的那样,您将使其工作,我只是展示了方法......

function FilterService() { 
}

FilterService.prototype.filter = function (companies, filter) {
 // this is our result variable which we will return at the end
 var result;
 // for test #1 the "filter" parameter is undefined and the test description says "returns original collection if called with undefined filter" (#2 will look similar to test #1, add this by yourself)
 if (typeof filter === "undefined") {
    result = companies;
 }
 // jump to test #4 we are talking about. "it" says "matches exact text on company name"
 companies.forEach( function (companyObj) {
   if (companyObj.name === filter) {
     result = [companyObj];
     return false;
   }
 });
 // continue to modify the function to meet all criteria of every "it" description

 // we return our result
 return result;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    相关资源
    最近更新 更多