小编Cha*_*hai的帖子

如何使用 Jest 模拟拒绝承诺

我在做什么?

我正在学习 Nestjs 课程,其中有一些单元测试。我编写了这个测试来检查存储库类中的signUp方法。问题是,为了触发异常,该行user.save()应该返回一个承诺拒绝(模拟写入数据库的一些问题)。我尝试了几种方法(见下文),但没有一个有效。

问题

结果是测试成功了,但是有一个unhandled Promise rejection. 这样,即使我断言它not.toThow()会以相同的方式成功unhandled Promise rejection

(node:10149) UnhandledPromiseRejectionWarning: Error: expect(received).rejects.toThrow()

Received promise resolved instead of rejected
Resolved to value: undefined
(Use `node --trace-warnings ...` to show where the warning was created)
Run Code Online (Sandbox Code Playgroud)

我如何让它正确拒绝承诺?

代码

下面是我的测试代码和被测函数。

import { ConflictException } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import { UserRepository } from './user.repository';

describe('UserRepository', () => {
  let userRepository: UserRepository;

  let authCredentialsDto: AuthCredentialsDto …
Run Code Online (Sandbox Code Playgroud)

unit-testing promise jestjs nestjs ts-jest

8
推荐指数
1
解决办法
3万
查看次数

Java:程序包cucumber.api.junit不存在

我一直在努力使它起作用。

我想建立一个测试运行器类,像这样

但是我得到这个错误:

错误:(3,26)Java:包 Cucumber.api.junit 不存在
错误:(10,10)Java:找不到符号
符号:类Cucumber

该类如下所示:

package nl.prvgld.sigma.aanvraagtitels.testutil;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
public class RunFeature {
}
Run Code Online (Sandbox Code Playgroud)

Pom.xml看起来像:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  <modelVersion>4.0.0</modelVersion>
  <groupId>nl.prvgld.sigma.aanvraagtitels</groupId>
  <artifactId>Aanvraagtitels</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Aanvraagtitels</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>3.2.0</version>
      </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java8</artifactId>
        <version>1.2.5</version>
    </dependency>
      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
      <dependency>
          <groupId>info.cukes</groupId>
          <artifactId>cucumber-core</artifactId>
          <version>1.2.5</version>
          <scope>test</scope>
      </dependency>
      <!-- …
Run Code Online (Sandbox Code Playgroud)

java junit intellij-idea cucumber maven

1
推荐指数
2
解决办法
8713
查看次数