我对多个模型对象中的某些属性有类似的规则,我想用自定义属性验证器替换它们,以避免单元测试中的代码重复.
我有我的财产验证人:
public class IntIdPropertyValidator: PropertyValidator
{
public IntIdPropertyValidator()
: base("Property {PropertyName} should be greater than 0")
{
}
protected override bool IsValid(PropertyValidatorContext context)
{
var value = (int)context.PropertyValue;
return value > 0;
}
}
Run Code Online (Sandbox Code Playgroud)
并在模型验证器类中连接它:
public class SomeRequestValidator : AbstractValidator<CreateWordRequest>
{
public SomeRequestValidator()
{
RuleFor(x => x.Id).SetValidator(new IntIdPropertyValidator());
}
}
Run Code Online (Sandbox Code Playgroud)
试图测试:
[Test]
public void Validate_IdHasValidator_Success()
{
Init();
validator.ShouldHaveChildValidator(x => x.Id, typeof(IntIdPropertyValidator));
}
Run Code Online (Sandbox Code Playgroud)
但测试总是失败.
那么,我如何测试实际为属性Id设置的验证器?
在有关gmock 的所有文档中,我总是发现要在测试中实例化的模拟对象,如下所示:
TEST(Bim, Bam)
{
MyMockClass myMockObj;
EXPECT_CALL(MyMockObj, foo(_));
...
}
Run Code Online (Sandbox Code Playgroud)
因此,每次测试都会创建和销毁对象。我相信创建和销毁每个测试装置的对象也完全没问题。但我想知道是否也可以拥有模拟对象的文件全局实例,如下所示:
MyMockClass myMockObj;
TEST(Bim, Bam)
{
EXPECT_CALL(MyMockObj, foo(_))
...
}
Run Code Online (Sandbox Code Playgroud)
我试过了,到目前为止我绝对没有问题,它似乎一切正常。但也许我应该知道什么?只是因为我偶然发现了这个问题,唯一的答案是:
...问题在于您正在实例化 FooMock 的全局实例。Googlemock/googletest 期望在测试主体内或测试夹具类内定义模拟。
但是我在文档或其他任何地方都找不到任何证实这一点的内容(我是否忽略了它?)。
谢谢,乔治
PS:我需要使用全局模拟实例的原因将是另一个讨论的主题(请参阅我的这篇帖子)。
所以我正在努力创建我的第一个单元测试。我正在使用 Google Test,该版本目前内置于 Visual Studio 2017 中。这是对概念验证的一次尝试,非常简单。
我在测试代码的解决方案中有一个项目:test.cpp
#include "pch.h"
#include "../ConsoleApplication1/ConsoleApplication1.cpp"
TEST(SumTest, FirstTest) {
EXPECT_EQ(6, sum(2, 4));
}
TEST(SumTest, SecondTest) {
EXPECT_EQ(1, 1);
EXPECT_TRUE(true);
}
Run Code Online (Sandbox Code Playgroud)
我有一个简单的方法可以在 ConsoleApplication1.cpp 中进行测试
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int sum(int a, int b);
int main()
{
int x = sum(2, 4);
printf("The answer is: %d \n", x);
}
int sum(int a, int b) {
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
一切都编译正常,但是当我从“测试资源管理器”菜单中单击“全部运行”时,我得到以下输出:
[5/7/2018 4:17:42 PM Informational] ------ …
Run Code Online (Sandbox Code Playgroud) 对于一个项目,我正在嵌入式系统上用 C++ 实现一个组件,该组件通过FreeRTOS 队列获取传感器数据并将其处理为FreeRTOS 任务。
由于硬件尚未到达和质量原因 (TDD),我想模拟 freeRTOS 功能并使用它们来模拟我的组件行为。
我提前谢谢你。
我正在使用C ++在Gtest和Gmock上对两个dll进行单元测试:
A.dll和B.dll,都是用C编写的,我无法修改它们。
A.dll init
函数通过函数指针将B.dll函数用作参数。我想模拟B的功能(因为它们是依赖于硬件的)。
我为A.dll创建了一个测试夹具类,该类可动态加载函数init
和calc
。以下代码提供了所需功能的快速概述:
class TestFixture : public ::testing::Test {
// dynamically loads functions from A.dll and assigns
// them to function pointers init_ptr and calc_ptr.
};
// function pointer typedef for use with B.dll's functions
typedef int (*funcPtr)(int, int);
// Loaded from A.dll
void init(funcPtr func1, funcPtr func2) {
// internally set functions in A.dll to be used in calculate
}
// Loaded from A.dll
int calculate(int a, int b) {
// …
Run Code Online (Sandbox Code Playgroud) public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags);
Run Code Online (Sandbox Code Playgroud)
我有一个测试器应用程序,用于我的ExtendedComboBox.以下代码中给出的所有String项目都存在于我的测试器应用程序中的ComboBox项目中.如何对上述方法进行单元测试,因为它返回void?测试TextRenderer.Drawtext的另一种方法是什么?是否有任何替代测试OnDrawItem方法来绘制ComboBox文本.
[TestMethod()]
public void ExtendedComboBoxOnDrawPrefixTest()
{
ExtendedComboBox cboTest = new ExtendedComboBox ();
// List of strings having special characters.
string[] items = {
"&One",
"T&wo",
"E!xclamation",
"Am@persat",
"H#ash",
"Dollar$Sign",
"Perc%ent",
"Ci^rcumflex",
"Ast*erisk",
"Hy-phen",
"Und_erscore",
"pl+us",
"Equ=als",
"Col:on",
"Semi;colon",
"Co'mma",
"Inverted\"Comma",
"Apos'trophe",
"Pip|e",
"Open{Brace",
"Close}Brace",
"OpenBr[acket",
"CloseBr]acket",
"BackS\\lash",
"ForwardSl/ash",
"LessT<han",
"Greate>rThan",
"Questio?nMark",
"Do.t",
"Three",
"Four",
"Five",
"Six",
"This is a really extremely long string value for …
Run Code Online (Sandbox Code Playgroud)