草庐IT

javascript - 我们如何在 Angular 2 中编写基本单元测试?

coder 2024-07-23 原文

我在官方网站上有以下 Angular 文档。但是在文档中测试部分已经过时并且不能使用当前的 Angular 2 beta 版本。我需要编写一个基本测试来检查 if 条件是否正常工作。我怎样才能在 Angular 2 中使用 jasmine 来做到这一点。

最佳答案

设置 jasmine 以使用 angular2 (beta.7) 运行 typescript 单元测试:

  1. 设置 Angular 项目
    (参见说明 5 分钟快速入门 https://angular.io/guide/quickstart )

    根目录是我的项目

  2. 使用 mpm 安装 jasmine

    npm install jasmine-core --save-dev --save-exact
    
  3. 安装实时服务器
    https://www.npmjs.com/package/live-server

  4. 获取语法/智能感知支持:
    在 myproject/typings 中创建一个新文件 jasmine.d.ts

    /// <reference path="jasmine\jasmine.d.ts" /> 
    


    获取jasmine.d.ts https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/jasmine/jasmine.d.ts

    并将其作为文件 jasmine.d.ts 保存在 myproject\typings\jasmine 中

  5. 将 unit-test.html 保存到我的项目中

    <html>
     <head>
       <title>Angular2: Jasmine Tests</title>
       <link rel="stylesheet" href="../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
       <script src="../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
       <script src="../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
       <script src="../node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
     </head>
     <body>
      <!-- #1. add the system.js library -->
      <script src="../node_modules/systemjs/dist/system.src.js"></script>
      <script>
           // #2. Configure SystemJS to use the .js extension
           //     for imports from the app folder
        System.config({
        packages: {
        'app': {defaultExtension: 'js'}
        }
      });
           // #3. Import the spec file explicitly
       System.import('app/app.spec')
           // #4. wait for all imports to load ...
           //     then re-execute `window.onload` which
           //     triggers the Jasmine test-runner start
           //     or explain what went wrong
      .then(window.onload)
      .catch(console.error.bind(console));
     </script>
       </body>
       </html>
    

    .then (window.onload) 对开始测试执行很重要。

    看这里Wait for a module to be loaded to execute the tests it contains

  6. 在目录 myproject\app 中创建新文件 app.spec.ts

    import {AppComponent} from './app.component';
    
    
    // Jasmin Test  App title property
    describe('AppComponent', () => {
        var app: AppComponent = null
    
        beforeEach(() => {
        app = new AppComponent();
        app.title = "App in Test";
        });
    
       it('should have an title property', () => {
    
          expect(app.title).toBeDefined();
       });
    
       it('should have the title App in Test', () => {
    
          expect(app.title).toBe("App in Test");
       })
    });
    
    
    // Jasmin Test without Angular
    describe("A suite", function() {
        it("contains spec with an expectation", function() {
            expect(true).toBe(true);
        });
    });
    
  7. 从命令行开始

    live-server --open=unit-test.html
    

这是我用 Angular 2 的 typescript 编写的 Jasmine 运行单元测试的工作设置。

如果您有任何错误,请按照 Günther 在他的评论中提出的建议发布您尝试过的内容和失败的地方。

关于javascript - 我们如何在 Angular 2 中编写基本单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35648175/

有关javascript - 我们如何在 Angular 2 中编写基本单元测试?的更多相关文章

  1. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  2. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  3. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  6. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  7. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  8. ruby - Ruby 的 Hash 在比较键时使用哪种相等性测试? - 2

    我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。

  9. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  10. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

随机推荐