gpt4 book ai didi

angularjs - Jasmine:为什么 beforeEach() 在嵌套描述中工作,而 beforeAll() 没有?

转载 作者:行者123 更新时间:2023-12-04 12:20:04 25 4
gpt4 key购买 nike

我正在努力理解为什么我的代码不起作用以及为什么当我使用一个简单的 beforeAll() 时测试会失败。而不是 beforeEach()在嵌套的描述测试套件中?这是一个概述我的问题的小例子:

describe("myService", function() {
// Basic services
// Some variables

beforeEach(module('app')); // Invoke the module
beforeEach(function(){

// Inject the services
inject(function(_myService_) {
myService = _myService_;
});
});

/************************ Add more unit tests here ************************/

describe("myFunction", function() {
describe("calls function with one set of input paramenters", function() {

//beforeAll(function() {
beforeEach(function() { // <-- Why this works but beforeAll doesn't???
// Call myFunction with different parameters
result = myService.myFunction(parametersType_1);
});

it("should do tests on result (the return from the function)", function() {
});
});

describe("calls function with other set of input paramenters", function() {

//beforeAll(function() {
beforeEach(function() { // <-- Why this works but beforeAll doesn't???
// Call myFunction with different parameters
result = myService.myFunction(parametersType_2);
});

it("should do tests on result (the return from the function)", function() {
});
});
});

最佳答案

将您注入(inject)服务的部分更改为 beforeAll 而不是 beforeEach:

beforeAll(function(){

// Inject the services
inject(function(_myService_) {
myService = _myService_;
});

外部 describe 中的 beforeEach 不会在每个嵌套的 describe 部分之前触发,而是在描述中的每个“it”之前触发。因为内部 beforeAll 在外部描述中的 beforeEach 之前被触发,所以您试图在注入(inject)之前使用该服务。

例如:
describe("outer describe", function() {

beforeAll(function() {
console.log("A");
});

beforeEach(function() {
console.log("B");
});

describe("inner describe", function() {
beforeAll(function() {
console.log("C");
});

beforeEach(function() {
console.log("D");
});

it("test", function() {
})'
});

});

将按顺序执行:A、C、B、D

关于angularjs - Jasmine:为什么 beforeEach() 在嵌套描述中工作,而 beforeAll() 没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37595158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com