- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我遇到了一个常见的场景。我需要在 beforeAll
或每个变量中创建一个变量。
describe('some test', () => {
beforeAll(() => {
const foo = createFoo({}, {});
});
it('returns something', () => {
// how to access foo?
});
});
如果我这样做,我将无法在 it
测试中访问 foo
,因为它只存在于 beforeAll
范围内。
为了能够访问我需要的地方,我必须在 describe
中声明 foo
:
describe('', () => {
let foo;
或者使用
this.foo =
这两种方法的问题是我丢失了类型信息。而且我没有针对此类函数的返回类型的显式接口(interface)。
有没有办法在某个地方声明 foo
以便我以后可以访问它并且不会丢失类型信息?
最佳答案
您可以使用 non-null assertion operator (!
) 放宽非空约束。
describe('some test', () => {
let foo!: SomeType;
beforeAll(() => {
foo = createFoo({}, {});
});
it('returns something', () => {
expect(foo.bar).toBe(true);
});
});
关于typescript - 如何在 beforeAll 测试中创建类型变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47192329/
我有一个带有数据库和 rabbitmq 用法的小型 Spring Boot 应用程序。 所以我想用集成测试(H2 + apache qpid)进行测试。 @ExtendWith(SpringExten
这是同一测试的 2 个样本。唯一的区别是第一个使用了 beforeAll 中的 promise 。块为变量赋值,而第二个块直接赋值。 我提出了类似的问题 Running spec after prom
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。 目前我必须在每个测试文件中包含这个beforeAll: import matchMediaPolyfill from 'm
我有一个使用 Jest 测试的 Node.js 项目。我有几个具有相同设置要求的测试文件。以前,所有这些测试都在一个文件中,所以我只有一个 beforeAll(...)执行通用设置。现在,随着测试分成
我正在为rest api spring boot编写单元测试。 @RunWith(SpringRunner.class) @SpringBootTest(classes = WorkflowEngin
给定我测试的类,其中有许多嵌套类: class TestWithManyNested { @RegisterExtension static MyExtension extension
我正在尝试通过测试将我的项目从使用 JUnit 4 升级到 JUnit5。升级后: @RunWith(VertxUnitRunner.class) --> @ExtendWith(VertxExten
我尝试过思考这个问题,阅读了一些问题,并且创建了一个小测试: describe("let", () => { let x = 1; it("",function() { console
我遇到了一个常见的场景。我需要在 beforeAll 或每个变量中创建一个变量。 describe('some test', () => { beforeAll(() => { const
我刚刚安装了Protractor v2.0.0 .我尝试添加 beforeAll测试并出错 ReferenceError: beforeAll is not defined 查看堆栈跟踪,我看到 ja
我想在 Eclipse Oxygen 4.7.3a 中从 JUnit 4 转换为 5。我认为添加 Jupiter 库就足够了:库、构建路径等。但是,@BeforeAll , @AfterAll , @
这是我所拥有的: spec :: Spec spec = do manager SpecWith a -> Spec 但我不知道如何从测试中访问经理。 spec :: Spec spec = b
在使用 Jasmine 测试我的 Meteor 应用程序的某个功能之前,我必须为测试准备不同的东西。因此我使用 beforeAll block 。 重置数据库 在数据库中创建一个讲座 在数据库中创建一
我正在使用 nodejs 为我们的 API 创建一个端到端的测试套件。在每次测试运行之前,我需要为该测试插入数据库记录。许多有问题的表没有使用 native 自动增量类型字段作为它们的主键。 (我知道
我正在使用 Jest-Puppeteer 对 Rails 应用程序进行端到端测试。在这些测试之前,我想运行一些种子并进行 DRY 我告诉服务器在每次测试之前转到某个 URL。 // imports d
我最近查看了一位同事的代码,我意识到他在 describe 调用顶部的 BeforeAll 函数中实现了一个 jest 函数,然后在 beforeEach 函数中创建了一个数据对象。这让我想知道,Be
为什么我们不应该将 beforeAll(function(){ let foo = 'test' }) 替换为 let foo = 'test' ?如果第二种方法没问题,beforeAll 的目的是什
目前 JUnit 5 API 只允许在静态方法上使用 @BeforeAll。 所以如果我做这样的事情,它不会编译: @BeforeAll fun setup() { MockitoAnno
我有一个简单的问题。我有这样的类结构:基类和子类(关系是继承)。 @BeforeAll 来自 JUnit5。 abstract class Base { static{ Sy
我正在尝试使用 @Transactional 来测试我的应用程序,以在测试后回滚,并使用 @BeforeAll 设置测试对象: @EnableJpaRepositories @SpringBootTe
我是一名优秀的程序员,十分优秀!