Unit testing 如何测试聚合物中的服务?

Unit testing 如何测试聚合物中的服务?,unit-testing,polymer,mocha.js,polymer-2.x,Unit Testing,Polymer,Mocha.js,Polymer 2.x,我正在写一个聚合应用程序,有一个聚合元素使用的服务。我想测试这项服务,但不知道如何测试 以下是我的服务: <script src="../../webcomponentsjs/webcomponents-lite.js"></script> <script src="../../web-component-tester/browser.js"></script> <link rel="import" href="../test-servic

我正在写一个聚合应用程序,有一个聚合元素使用的服务。我想测试这项服务,但不知道如何测试

以下是我的服务:

<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>

<link rel="import" href="../test-service.html">

<script>
define('test-service', () => {
  class TestService {
    constructor(componentA, componentB) {
      this.componentA = componentA;
      this.componentB = componentB;
    }
  }
  return TestService;
});
</script>

定义('test-service',()=>{
类测试服务{
构造函数(组件A、组件B){
this.componentA=componentA;
this.componentB=componentB;
}
}
返回测试服务;
});

我如何测试这个?如果我只是尝试包含.html文件,我就无法访问TestService。

这是示例测试的完整html。基本上,通过
testfixture
添加您的测试服务标签,然后查看它是否正常工作

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

    <title>test-service test</title>

    <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
    <script src="../../web-component-tester/browser.js"></script>

    <link rel="import" href="../test-service.html">
</head>
<body>

<test-fixture id="BasicTestFixture">
    <template>
        <test-service></test-service>
    </template>
</test-fixture>

<script>
    suite('test-service', function() {

        test('instantiating the element with default properties works', function() {
            let testService = fixture('BasicTestFixture');
            assert.equal(testService.apiUrl, 'http://myapi.domain.com');
            // possible something like
            // let addResult = testService.addElement(...);
            // assert.equals(addResult, '{"result": "success"}');
        });

    });
</script>

</body>
</html>

测试服务测试
套件('test-service',function()){
test('实例化具有默认属性的元素有效',function(){
设testService=fixture('BasicTestFixture');
assert.equal(testService.apirl,'http://myapi.domain.com');
//可能是
//让addResult=testService.addElement(…);
//等于(addResult,{“result”:“success”});
});
});

终于明白了。这一切都与聚合物IMD和依赖注入有关。定义一个测试套件与它看起来不同:

define('test-service-test', ['test-service'], (TestService) => {
    let testServiceInstance = new TestService(1, 2);

    test('basic test', function(){
        assert.equal(testServiceInstance.componentA, 1);
        assert.equal(testServiceInstance.componentB, 2);
    })
});

你试过
web组件测试仪吗?
?是的,这就是我正在使用的,也是我的问题的根源。我相应地更新了我的问题描述。