Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 单元测试Jasmine Typescript,分离关注点_Unit Testing_Typescript_Jasmine - Fatal编程技术网

Unit testing 单元测试Jasmine Typescript,分离关注点

Unit testing 单元测试Jasmine Typescript,分离关注点,unit-testing,typescript,jasmine,Unit Testing,Typescript,Jasmine,我制作了一个hello world应用程序,当按下按钮时,它会从我的API中获取字符串“hello world” 我有一些单元测试的经验,我想知道我是否正确地分离了关注点 我计划进行的测试包括: 测试按钮click是否只调用GET方法一次 测试函数是否返回字符串helloworld 因此,我的问题是: 是否可以检查调用函数的次数 实现该功能的控制器位于单独文件中的typescript中,有人能告诉我测试typescript控制器功能的正确方向,以及如何在TS中编写jasmine测试 谢谢,在ja

我制作了一个hello world应用程序,当按下按钮时,它会从我的API中获取字符串“hello world”

我有一些单元测试的经验,我想知道我是否正确地分离了关注点

我计划进行的测试包括:

  • 测试按钮click是否只调用GET方法一次
  • 测试函数是否返回字符串helloworld
  • 因此,我的问题是:

  • 是否可以检查调用函数的次数
  • 实现该功能的控制器位于单独文件中的typescript中,有人能告诉我测试typescript控制器功能的正确方向,以及如何在TS中编写jasmine测试

  • 谢谢,

    在jasmine 2.x中,可以像这样检查函数被调用的次数(当然,这是假设您正在监视所述函数):

    在TS中编写Jasmine测试与JS非常相似。据我所知,唯一真正的区别是语法。现在,您将拥有强大的变量类型,而不是使用fat arrow语法执行it和用函数描述块。所以不是

    describe('Test', function() {
        it('true is true', function() {
            expect(true).toBe(true);
        })
    })
    
    您将这样编写测试

    describe('Test', () => {
        it('true is true', () => {
            expect(true).toBe(true);
        })
    })
    
    这是我在TS中编写的一个小的人为测试样本

    namespace UserList.Services.Tests {
    
    describe("UserList: GetEntityTypeService", () => {
        let service: UserList.Services.GetEntityTypeService;
    
        beforeEach(() => {
            angular.mock.module("App.UserList.Module");
    
            angular.mock.module(($provide: angular.auto.IProvideService) => {
                $provide.service("UserList.Services.GetEntityTypeService", UserList.Services.GetEntityTypeService);
            });
    
            inject(($injector: angular.auto.IInjectorService) => {
                service = <UserList.Services.GeteEntityTypeService>($injector.get("UserList.Services.GetEntityTypeService"));
            });
        });
    
        it("should be defined", () => {
            expect(service).toBeDefined();
        });
    });
    
    namespace UserList.Services.Tests{
    描述(“UserList:GetEntityTypeService”,()=>{
    let服务:UserList.Services.GetEntityTypeService;
    在每个之前(()=>{
    angular.mock.module(“App.UserList.module”);
    angular.mock.module($provide:angular.auto.iproviservice)=>{
    $provide.service(“UserList.Services.GetEntityTypeService”,UserList.Services.GetEntityTypeService);
    });
    注入($injector:angular.auto.IInjectorService)=>{
    服务=($injector.get(“UserList.Services.GetEntityTypeService”);
    });
    });
    它(“应该定义”,()=>{
    expect(service.toBeDefined();
    });
    });
    
    }

    记住TS是JS的超集。因此,任何有效的JS都是有效的TS。如果您真的愿意,您可以用纯JS编写测试,它仍然可以工作

    至于你关于分离关注点的问题,我认为你的思路是正确的。这里有一个我遵循的模式,它非常有效

    • 用类型和名称描述对象
    • 加载对象的模块
    • 根据需要加载模拟模块
    • 注入依赖项并监视方法
    • 初始化对象:
      • 服务只是需要注入
      • 使用$controller服务实例化控制器
      • 我们需要编译指令
    • 编写分组在描述块中的期望值

    测试状态、对服务的同步和异步调用、事件以及必要时的预期结果。

    在jasmine 2.x中,可以这样检查函数被调用的次数(当然,这是假设您正在监视所述函数):

    在TS中编写Jasmine测试与JS非常相似。据我所知,唯一真正的区别是语法。现在,您将拥有强大的变量类型,而不是使用fat arrow语法执行it和用函数描述块。所以不是

    describe('Test', function() {
        it('true is true', function() {
            expect(true).toBe(true);
        })
    })
    
    您将这样编写测试

    describe('Test', () => {
        it('true is true', () => {
            expect(true).toBe(true);
        })
    })
    
    这是我在TS中编写的一个小的人为测试样本

    namespace UserList.Services.Tests {
    
    describe("UserList: GetEntityTypeService", () => {
        let service: UserList.Services.GetEntityTypeService;
    
        beforeEach(() => {
            angular.mock.module("App.UserList.Module");
    
            angular.mock.module(($provide: angular.auto.IProvideService) => {
                $provide.service("UserList.Services.GetEntityTypeService", UserList.Services.GetEntityTypeService);
            });
    
            inject(($injector: angular.auto.IInjectorService) => {
                service = <UserList.Services.GeteEntityTypeService>($injector.get("UserList.Services.GetEntityTypeService"));
            });
        });
    
        it("should be defined", () => {
            expect(service).toBeDefined();
        });
    });
    
    namespace UserList.Services.Tests{
    描述(“UserList:GetEntityTypeService”,()=>{
    let服务:UserList.Services.GetEntityTypeService;
    在每个之前(()=>{
    angular.mock.module(“App.UserList.module”);
    angular.mock.module($provide:angular.auto.iproviservice)=>{
    $provide.service(“UserList.Services.GetEntityTypeService”,UserList.Services.GetEntityTypeService);
    });
    注入($injector:angular.auto.IInjectorService)=>{
    服务=($injector.get(“UserList.Services.GetEntityTypeService”);
    });
    });
    它(“应该定义”,()=>{
    expect(service.toBeDefined();
    });
    });
    
    }

    记住TS是JS的超集。因此,任何有效的JS都是有效的TS。如果您真的愿意,您可以用纯JS编写测试,它仍然可以工作

    至于你关于分离关注点的问题,我认为你的思路是正确的。这里有一个我遵循的模式,它非常有效

    • 用类型和名称描述对象
    • 加载对象的模块
    • 根据需要加载模拟模块
    • 注入依赖项并监视方法
    • 初始化对象:
      • 服务只是需要注入
      • 使用$controller服务实例化控制器
      • 我们需要编译指令
    • 编写分组在描述块中的期望值
    测试对服务和事件的状态、同步和异步调用,以及必要时的预期结果