如何使用CucumberJS要求Jasmine Library的全部?

问题描述 投票:0回答:1

我想对Angular v9应用程序使用Jasmine和CucumberJS进行单元测试。在tutorial from cucumber.io之后,我将黄瓜设置为默认运行器。但是无法使用茉莉花方法。弄清楚如何使用Expect方法,但需要访问Jasmine库的其余部分,例如spyOncreateSpyObj。当我执行以下行时:expect(this.actualAnswer).toBe(expectedAnswer);引发错误,提示TypeError: Cannot read property 'toBe' of undefined at World.<anonymous> (C:\Users\User\cukejas\features\step_definitions\stepdefs.js:24:28)

这是我到目前为止所拥有的。有人可以帮忙吗?

stepdefs.js

const assert = require('assert');
const { Given, When, Then } = require('cucumber');
const expect = require('C:\\Users\\User\\cukejas\\node_modules\\jasmine\\lib\\jasmine.js');

function isItFriday(today) {
  if (today === "Friday") {
    return "TGIF";
  } else {
    return "Nope";
  }
}

Given('today is {string}', function (givenDay) {
  this.today = givenDay;
});

When('I ask whether it\'s Friday yet', function () {
  this.actualAnswer = isItFriday(this.today);
});

Then('I should be told {string}', function (expectedAnswer) {
  // assert.equal(this.actualAnswer, expectedAnswer);
  //expect(1); --> This line executes without error
  expect(this.actualAnswer).toBe(expectedAnswer); // error thrown here
});

package.json

{
  "name": "cukejas",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "./node_modules/.bin/cucumber-js -p default",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.1",
    "@angular/common": "~9.0.1",
    "@angular/compiler": "~9.0.1",
    "@angular/core": "~9.0.1",
    "@angular/forms": "~9.0.1",
    "@angular/platform-browser": "~9.0.1",
    "@angular/platform-browser-dynamic": "~9.0.1",
    "@angular/router": "~9.0.1",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.2",
    "@angular/cli": "~9.0.2",
    "@angular/compiler-cli": "~9.0.1",
    "@angular/language-service": "~9.0.1",
    "@types/cucumber": "^6.0.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "cucumber": "^6.0.5",
    "cucumber-pretty": "^6.0.0",
    "cucumber-tsflow": "^3.2.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "typescript": "~3.7.5"
  }
}
typescript jasmine cucumber cucumberjs
1个回答
0
投票

Jasmine和CucumberJS都是测试运行程序,因此很遗憾,您不能同时使用两个框架。但是,您可以使用expect程序包(Jest的一部分),该程序包具有类似的API:

const expect = require('expect');

在线示例:https://testjam.io/?p=D11o5cwOIQukGy1F6GRU

或者,chai是另一个很棒的库,支持表达性断言。

spyOn而言,您可以考虑使用独立的库,例如sinon

© www.soinside.com 2019 - 2024. All rights reserved.