在角度2测试中伪造模块 [英] Faking a module in angular 2 test

查看:100
本文介绍了在角度2测试中伪造模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有角度2服务的功能,我想测试。

I have a function in angular 2 service which I would like to test.

service.ts

upload(){
  let file = new Transfer();
  file.upload(myfile).then( // my callback );
}

我想模拟转移在我的测试中使用 jasmine 。我试试了这个

I would like to mock Transfer in my test using jasmine. I tried this in my

sevice.spec.ts

从'../ mocks / mocks'导入{TransferMock as Transfer}来模拟它。但它没有用。这就是我的测试实例化的方式。

import { TransferMock as Transfer } from '../mocks/mocks' to mock it. But it is not working. This is how my test is instantiated .

describe('authentication service' , () => {
  beforeEach(() => {
    auth = new Service(<any>new HttpMock(), <any>new StorageMock())
  });
  it('initialize authentication',() => {
    expect(auth).not.toEqual(null);
    auth.upload('file'); //it fails here
  });
})

编辑

转移未在服务中注入。只有一个函数使用转移。所以不注入可以减少应用程序的初始加载时间我猜(很高兴知道其他意见)。所以我想知道是否有嘲笑它是否以这种方式构建?

Transfer is not injected in the service. Only one function uses Transfer . So not injecting can reduce the initial loading time of the app i guess(would be happy to know other opinions) . So I would like to know if there is anyway to mock if its constructed this way ?

编辑

虽然我接受了马丁的回答,因为这是最好的做法,当你使用 ionic-native 插件时,它有一个问题。如果插件没有浏览器支持,它可能会失败。在这种情况下,它发生在我注入它时,错误未定义FileTransfer 。所以我又回来了,寻找建议。

Although I had accepted Martin's answer as it is the best practice, it has one issue which can happen when you use ionic-native plugins.If the plugin doesnt have browser support it can fail. In this case it happened when I inject it, with error FileTransfer is not defined . So I am back again, looking for suggestions.

推荐答案

为了在测试中为类提供模拟,你需要在您的实现中注入类。

In order to provide a mock for a class in a test, you need to inject the class in your implementation.

ngModule 中添加转移到您的提供商。然后只需将其注入您的服务。

In your ngModule add Transfer to your providers. Then simply inject it into your service.

然后在您的测试中,您可以在<$ c $中使用 {provide:Transfer,useClass:TransferMock} c> TestBed 提供者。

Then in your test you can use { provide: Transfer, useClass: TransferMock } in your TestBed providers.

更新

依赖注入的主要目的是使代码可测试并且允许嘲笑 - 假装 - 服务的存根。

The primary purpose of Dependency Injection is to make code testable and to allow mocking - faking - stubbing of services.

更新

使用Dependancy Injection,您可以配置一组不同的提供商不同的环境。

With Dependancy Injection you can configure a different set of providers for different environments.

例如,如果您在浏览器中运行应用程序,并且在本机移动环境中,您可以更换配置。

For example, if you are running your application in the browser, and in a native mobile environment you can swap out your configuration.

在你的模块中你可以有这样的东西:

In your module you could have something like this:

const TRANSFER_PROVIDER: any;

if (environment.browser) {
  TRANSFER_PROVIDER = Transfer;
} else {
  TRANSFER_PROVIDER = { provide: Transfer, useClass: NativeTransfer }
}

...
providers: [ TRANSFER_PROVIDER ]

NativeTransfer可以是一个简单的存根,只能防止错误,或者它可以让用户知道这个功能是他们的浏览器不支持。

NativeTransfer could be a simple stub that does nothing but prevent errors, or it could let the user know that this feature is not supported in their browser.

这篇关于在角度2测试中伪造模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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