- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
尝试模拟在另一个方法中调用的方法。
// code part
public virtual bool hello(string name, int age)
{
string lastName = GetLastName();
}
public virtual string GetLastName()
{
return "xxx";
}
// unit test part
Mock<Program> p = new Mock<Program>();
p.Setup(x => x.GetLastName()).Returns("qqq");
我希望 GetLastName 方法始终返回“qqq”。
最佳答案
这应该有效,假设这些是完整的方法实现
public class MyProgram
{
public bool hello(string name, int age)
{
string lastName = GetLastName();
return string.Format("hello {0}", lastName);
}
public virtual string GetLastName()
{
return "xxx";
}
}
public class MyProgramTests
{
[TestMethod]
public void MyTest()
{
string stringToReturn = "qqq";
Mock<MyProgram> name = new Mock<MyProgram>();
name.CallBase = true;
name.Setup(x => x.GetLastName()).Returns(stringToReturn );
var results = name.Object.hello(It.IsAny<string>(), It.IsAny<int>());
string expected = string.Format("hello {0}", results);
Assert.AreEqual(expected, results);
}
}
我还是不太理解你的评论:
What does the parameter really mean of Mock What should ?? be? excuse me, I don't quite understand the syntax. To clarify, mock means that when I put break points in my code I the breakpoints should skip the methods that I am mocking. Am I right?
Mock<T>
允许你模拟 T
的类型- T
作为一个通用指标,也意味着几乎所有属于类的东西。在传统中,你会 mock interface
,不是实际的 class
,但在上面的示例中,我们正在模拟一个类。对于发布的示例单元测试,单元测试的目的是测试 hello(string, int)
的实现。 .我们知道 hello(string, int)
依赖于该类中的另一个方法 GetLastName()
. GetLastName()
s 的实现虽然重要,但对单元测试的范围并不重要 hello(string, int)
.为此,我们模拟调用及其返回 - 以测试 hello(string, int)
的功能。无需担心其依赖项的实现。
我用实际的类名包围了上面的内容,希望能更明显地表明我们正在模拟类 MyProgram
并提供 GetLastName()
的新实现(模拟)
Thanks for the answer. What if I want to test a method that calls another method that calls another method? For eg. what if the method hello called another method?
同样的原则适用,当你构建你的单元测试时(假设它们是单元测试,而不是集成测试或其他,你总是想专注于测试一个 公共(public)方法。What's the difference between unit and integration tests?
public class Foo
{
public string Bar()
{
return string.Format("{0}Bar", Baz(5));;
}
public virtual string Baz(int someNumber)
{
return string.Format("{0}Baz", DoStuff(someNumber).ToString());
}
public virtual int DoStuff(int someNumber)
{
return someNumber+1;
}
}
如果我们进行单元测试 Bar()
我们不关心 Baz(int)
的执行情况甚至更糟DoStuff(int)
.请注意,我们不关心实现,我们确实关心它们的返回值。来自 Bar()
从我们的角度来看,唯一重要的是 Baz(int)
返回一个字符串。什么字符串? Bar()
没关系单元测试。
Bar()
的样本测试:
[TestMethod]
public void Bar_ReturnsBazValueWithBarAppended
{
// Arrange
string testBazReturn = "test";
Mock<Foo> mock = new Mock<Foo>();
mock.CallBase = true;
mock
.Setup(s => s.Baz(It.IsAny<int>())
.Returns(testBazReturn);
// Act
var results = mock.Object.Bar();
// Assert
Assert.AreEqual(string.Format("{0}{1}", testBazReturn, "Bar"), results);
mock.Verify(v => v.Baz(It.IsAny<int>())); // Verifies that Baz was called
}
注意上面,我们实际实现了Baz(int)
, 和 DoStuff(int)
没关系,因为我们忽略了 Baz(int)
的实际执行, 和 DoStuff(int)
甚至没有发挥作用。
现在,如果我们要测试 Baz(int)
我们只是遵循同样的心态:
[TestMethod]
public void Baz_ReturnsDoStuffValueWithBazAppended
{
// Arrange
int testDoStuffReturn = 1;
Mock<Foo> mock = new Mock<Foo>();
mock.CallBase = true;
mock
.Setup(s => s.DoStuff(It.IsAny<int>())
.Returns(testDoStuffReturn);
// Act
var results = mock.Object.Baz(5);
// Assert
Assert.AreEqual(string.Format("{0}{1}", results, "Baz"), results); // Validates the result
mock.Verify(v => v.DoStuff(It.IsAny<int>())); // Verifies that DoStuff was called
}
在上面,现在我们正在单元测试 Baz(int)
, 我们不关心 Bar()
,我们唯一关心的是 DoStuff(int)
是它返回一个值(但不是如何它到达该值。)
最后 DoStuff(int)
:
[TestMethod]
public void DoStuff_ReturnsParameterPlusOne()
{
// Arrange
Foo foo = new Foo();
int passed = 1;
int expected = passed + 1;
// Act
var results = foo.DoStuff(passed);
// Assert
Assert.AreEqual(expected, results);
}
关于c# - 模拟测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36345282/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!