如何在 Jasmine 测试中测试 $scope?

     2023-02-23     80

关键词:

【中文标题】如何在 Jasmine 测试中测试 $scope?【英文标题】:How to test $scope in Jasmine test? 【发布时间】:2016-09-14 14:47:20 【问题描述】:

我尝试使用 Jasmine 为 Angularjs 编写单元测试。 这是我的控制器:

function HomeController($scope, fav, news, materials) 
    console.log('home controller');
    $scope.testMe = true;


module.controller('HomeController', HomeController);

和测试

describe('Home controller tests', function() 
    var $rootScope, $scope, controller;

    beforeEach(function() 
        module('ap.designer');

        inject(function($injector) 
            $rootScope = $injector.get('$rootScope');
            $scope = $rootScope.new();
            controller = $injector.get('$controller')('HomeController', $scope: $scope);
        );
    );

    describe('test controller functions', function() 
        it('Should return true', function() 
            expect($scope.testMe).toBe(true);
        );
     );
);

即使我尝试测试,测试也失败了 expect(true).toBe(true);

Jasmine、Karma、Angular 和 Angular-mocks 位于我的 jasmine 调试页面的 index.html 中,还有带有测试的脚本。

我发现如果我删除 beforeEach() 块,expect(true).toBe(true) 就通过了。

这是一个错误:

minErr/<@http://localhost:9876/base/bower_components/angular/angular.js:68:12
forEach@http://localhost:9876/base/bower_components/angular/angular.js:322:11
loadModules@http://localhost:9876/base/bower_components/angular/angular.js:4548:5
createInjector@http://localhost:9876/base/bower_components/angular/angular.js:4470:19
workFn@http://localhost:9876/base/bower_components/angular-mocks/angular-mocks.js:2954:44
angular.mock.inject@http://localhost:9876/base/bower_components/angular-mocks/angular-mocks.js:2934:35
@http://localhost:9876/base/src/js/modules/ap.designer/test/controllers/home/HomeControllerSpec.js:12:9
window.__karma__.loaded@http://localhost:9876/debug.html:42:9
@http://localhost:9876/debug.html:78:5

【问题讨论】:

如果expect(true).toBe(true); 确实失败了,则表明beforeEach() 中出现了错误。你能检查一下浏览器开发工具来确定吗? 这是一个错误pastebin.com/BSwtJpc7 【参考方案1】:

检查您的模块依赖项。可能你的依赖项之一没有加载到 karma 配置文件部分,所以你的模块创建失败。

【讨论】:

如何在 jasmine 中编写单元测试用例?

】如何在jasmine中编写单元测试用例?【英文标题】:Howtowriteunittestcaseinjasmine?【发布时间】:2021-07-1110:27:36【问题描述】:Flag(a)letelement=this.selected.filter(item=>item.a===a)returnelement.length>1?true:false;不确定如何为上述函数编写单元... 查看详情

如何在 Jasmine 中使用 React 测试工具

】如何在Jasmine中使用React测试工具【英文标题】:HowtouseReactTestUtilitieswithJasmine【发布时间】:2014-03-3012:53:09【问题描述】:我用React的测试工具编写了单元测试代码。但是遇到了问题我的环境是:导轨4茉莉花2.0.0主干1.1.2describe("... 查看详情

你如何在 Jasmine 的 onShow 中测试 Backbone Marionnette 方法?

】你如何在Jasmine的onShow中测试BackboneMarionnette方法?【英文标题】:HowdoyoutestaBackboneMarionnettemethodinsideofonShowinJasmine?【发布时间】:2014-07-2019:45:05【问题描述】:我正在尝试在BackboneMarionette中测试一个视图,但onShow()从未被调用,... 查看详情

如何在 Angular 中使用 Jasmine 测试 RxJS switchMap?

】如何在Angular中使用Jasmine测试RxJSswitchMap?【英文标题】:HowtotestRxJSswitchMapwithJasmineinAngular?【发布时间】:2022-01-1611:45:10【问题描述】:在我的Angular项目中,我的一个组件中有这段代码:delete(post:PostInterface):voidconstdelete$=this.app... 查看详情

如何在 Jasmine 单元测试中使用 Sinon 对 jQuery 动画进行假时间?

】如何在Jasmine单元测试中使用Sinon对jQuery动画进行假时间?【英文标题】:HowdoIfake-timeajQueryanimationusingSinoninaJasmineunittest?【发布时间】:2011-11-1113:10:27【问题描述】:我有一个1秒的jQuery.animate操作,在页面加载后5秒启动。我在Ja... 查看详情

如何模拟在 AngularJS Jasmine 单元测试中返回承诺的服务?

】如何模拟在AngularJSJasmine单元测试中返回承诺的服务?【英文标题】:HowdoImockaservicethatreturnspromiseinAngularJSJasmineunittest?【发布时间】:2014-07-0511:36:08【问题描述】:我有myService使用myOtherService,它进行远程调用,返回承诺:angula... 查看详情

Jasmine(通过 TypeScript)测试以验证范围 $on 被调用

】Jasmine(通过TypeScript)测试以验证范围$on被调用【英文标题】:Jasmine(viaTypeScript)testtoverifyscope$oniscalled【发布时间】:2016-04-2115:43:45【问题描述】:我搜索了几个地方来获得这方面的想法。HowdoIunittest$scope.broadcast,$scope.$onusingJasm... 查看详情

Angular 7:如何在 Jasmine 单元测试中解决 Hammer.js 依赖项

】Angular7:如何在Jasmine单元测试中解决Hammer.js依赖项【英文标题】:Angular7:HowtoresolveHammer.jsdependencyinJasmineunittest【发布时间】:2020-11-0306:31:24【问题描述】:在为使用MaterialSlider并依赖HammerJS的组件编写单元测试时,我看到此错... 查看详情

在 Nodejs 中使用 Jasmine 测试 promise 是不是被解决或拒绝

】在Nodejs中使用Jasmine测试promise是不是被解决或拒绝【英文标题】:TestifapromiseisresolvedorrejectedwithJasmineinNodejs在Nodejs中使用Jasmine测试promise是否被解决或拒绝【发布时间】:2015-01-2516:21:08【问题描述】:我知道如何在Mocha中做到这... 查看详情

如何更好地构建 Jasmine 单元测试,使它们不会出现在一个巨大的文件中?

】如何更好地构建Jasmine单元测试,使它们不会出现在一个巨大的文件中?【英文标题】:HowtobetterstructureJasmineunittestssuchthattheydon\'tendupinahugefile?【发布时间】:2019-08-1900:28:01【问题描述】:我想确保我的单元测试可读且可维护,... 查看详情

在 Jasmine 单元测试中模拟 AngularJS 模块依赖项

】在Jasmine单元测试中模拟AngularJS模块依赖项【英文标题】:MockingAngularJSmoduledependenciesinJasmineunittests【发布时间】:2013-07-0710:38:36【问题描述】:我试图在一个模块中对控制器代码进行单元测试,该模块将其他模块作为依赖项,... 查看详情

在 Angular 中使用 Jasmine 使用 *ngIf 指令时,如何对元素是不是可见进行单元测试

】在Angular中使用Jasmine使用*ngIf指令时,如何对元素是不是可见进行单元测试【英文标题】:HowdoIunittestifanelementisvisiblewhenthe*ngIfdirectiveisusedusingJasmineinAngular在Angular中使用Jasmine使用*ngIf指令时,如何对元素是否可见进行单元测试【... 查看详情

如何在退出 Jasmine 测试之前测试 Observable 是不是已发布事件?

】如何在退出Jasmine测试之前测试Observable是不是已发布事件?【英文标题】:HowtotestifanObservablehaspublishedaneventornotbeforeexitingaJasminetest?如何在退出Jasmine测试之前测试Observable是否已发布事件?【发布时间】:2020-10-1422:08:56【问题描... 查看详情

如何在 (jasmine + karma) 中为以下方法编写测试,该方法在构造函数中注入了 ComponentFactoryResolver 和 ApplicationRef

】如何在(jasmine+karma)中为以下方法编写测试,该方法在构造函数中注入了ComponentFactoryResolver和ApplicationRef【英文标题】:HowdoIwriteatestforthefollowingmethodin(jasmine+karma)thathasComponentFactoryResolver&ApplicationRefinjectedintheconstructor【发布时 查看详情

如何在 Visual Studio Code 中使用 Typescript 和 Jasmine 框架编写 Protractor 测试脚本?

】如何在VisualStudioCode中使用Typescript和Jasmine框架编写Protractor测试脚本?【英文标题】:HowtowriteProtractortestscriptsusingTypescriptalongwithJasmineframeworkinVisualstudioCode?【发布时间】:2017-01-1321:07:31【问题描述】:我的项目正在从独立到Web... 查看详情

如何在不使用 Angular 的 spyOn 的情况下检查服务中的方法是不是在 Jasmine 单元测试中被调用?

...在不使用Angular的spyOn的情况下检查服务中的方法是不是在Jasmine单元测试中被调用?【英文标题】:HowtocheckifamethodfromaserviceiscalledornotinJasmineunittestingwithoutusingspyOninAngular?如何在不使用Angular的spyOn的情况下检查服务中的方法是否在J... 查看详情

如何对 DOM 操作进行单元测试(使用 jasmine)

】如何对DOM操作进行单元测试(使用jasmine)【英文标题】:howtounittestDOMmanipulation(withjasmine)【发布时间】:2013-04-1608:14:13【问题描述】:我需要用jasmine对一些DOM操作函数进行单元测试(目前我在浏览器和Karma中运行我的测试)我... 查看详情

如何编写一个期望在 Jasmine 中抛出“错误”的测试?

】如何编写一个期望在Jasmine中抛出“错误”的测试?【英文标题】:HowcanIwriteatestwhichexpectsan\'Error\'tobethrowninJasmine?【发布时间】:2011-05-0721:05:06【问题描述】:我正在尝试为JasmineTestFramework编写一个测试,它预计会出错。目前我... 查看详情