我需要存根的方法的调用方式如下:
List<DocumentInfo> documentInfosToDelete = await _documentInfoRepository.GetListByExternalIdAsync(
partyLegalEntity,
externalId,
type,
status);
Run Code Online (Sandbox Code Playgroud)
这有效,但会生成编译器警告:“此异步方法缺少‘等待’运算符”等。
testService.DocumentInfoRepos.GetListByExternalIdAsyncStringStringDocumentTypeDocumentStatus =
(async (a, b, c, d) =>
{
GetListByExternalIdAsyncCalled = true;
return new List<DocumentInfo>();
});
Run Code Online (Sandbox Code Playgroud)
我想摆脱这些警告,因为它们会淹没任何有关实际问题的警告。
返回 new Task(...) 挂起。如果我正确理解了我在 WWW 上找到的内容,Task.Run(...) 所做的事情与我们正在使用的等待异步模式完全不同。
该怎么办?
我正在学习Python,因为我认为它是一种非常强大且强大的语言,如C++,perl或C#,但同时真的很容易.我正在使用JetBrains的Pycharm,当我定义一个函数时,它要求我添加一个"Documentation String Stub",当我点击yes时它会添加如下内容:
"""
"""
Run Code Online (Sandbox Code Playgroud)
所以函数的完整代码是这样的:
def otherFunction(h, w):
"""
"""
hello = h
world = w
full_word = h + ' ' + w
return full_word
Run Code Online (Sandbox Code Playgroud)
我想知道这些(""""")符号是什么意思,谢谢.
我有以下代码:
Group.where('name ~* ?', params[:name]).first
Run Code Online (Sandbox Code Playgroud)
where
在那种情况下如何存根方法?
Group.stub(:where).and_return(mock_model(Group, name: "SomeName"))
Run Code Online (Sandbox Code Playgroud)
导致错误:
Mock "Group_1001" received unexpected message :first with (no args)
Run Code Online (Sandbox Code Playgroud) 我知道
Assert.IsFalse(postsPageOne.Intersect(postsPageTwo).Any());
Run Code Online (Sandbox Code Playgroud)
你可以比较对象来找到任何重复.
但是我想在我的方法中使用它之后检查我的列表是否包含重复项.这是测试代码:
///ARRANGE
///
var toShuffle = new List<int>(){
1001,
1002,
1003,
1004,
1005,
1006,
1007,
1008,
1009,
1010
};
///ACT
///
toShuffle = Shared_Components.SharedComponents.Shuffle(toShuffle, 10);
///ASSERT
///
Assert.IsTrue(toShuffle.Count == 10, "Number of Elements not correct!");
Assert.IsTrue(toShuffle.All(a => a >= 1001 && a <= 1010), "Elements out of range!");
Run Code Online (Sandbox Code Playgroud) 我正在尝试模拟 Node 模块中的一个函数。但它不允许我。有任何想法吗?
// module A
function foo(){
return 1;
}
function bar(){
return foo() + 1;
}
module.exports = {foo, bar}
Run Code Online (Sandbox Code Playgroud)
在测试...
const a = require('a');
...
sinon.stub(a, 'foo').callsFake(() => 3);
expect(a.bar()).to.equal(4); // gets 2 instead of 4
Run Code Online (Sandbox Code Playgroud) 我正在学习 Mockito 并尝试使用 Stub() 方法。我有一个简单的代码,但它不起作用,因为我收到此错误:“SpyTest 类型的方法存根(int)未定义”。我想知道我应该在pom文件中添加什么依赖项才能使用这个stub()方法?先感谢您!
这是代码:
package com.dgs.mockito;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
class SpyTest {
@Test
void test() {
List arrayListMock = mock(ArrayList.class);
assertEquals(0, arrayListMock.size());
stub(arrayListMock.size()).toReturn(5);
}
}
Run Code Online (Sandbox Code Playgroud)
这是 pom 文件:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dgs.mockito</groupId>
<artifactId>mockito-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.21.0</version> …
Run Code Online (Sandbox Code Playgroud) 仅当我尝试通过 IntelliJ 调试器调试测试时才会出现此问题。当我只是简单地运行测试时,这种情况不会发生。
CustomerChoiceRepository 是一个普通的 Spring Boot JPA 存储库,此处使用@Mock
.
当此行在调试器中执行时,我在变量的监视部分中收到以下错误:
整个错误信息是:
Method threw 'org.mockito.exceptions.misusing.UnfinishedStubbingException' exception. Cannot evaluate com.item.repository.jpa.CustomerChoiceRepository$MockitoMock$1318657964.toString()
Run Code Online (Sandbox Code Playgroud)
同样,这仅在 IntelliJ 调试器中检测到,因此仅当我调试它时测试才会失败。
所以我的问题是:这里发生了什么?
这是一个错误吗?这是我无法理解的事情,因为我不太了解 Mockito 的内部结构吗?