C# 避免在单元测试(NUnit)中运行函数
假设我有一门课:C# 避免在单元测试(NUnit)中运行函数,c#,unit-testing,nunit,nsubstitute,C#,Unit Testing,Nunit,Nsubstitute,假设我有一门课: public class SomeClass { public Model _model; public SomeClass() { _model = new Model(); } public void Foo() { _model.DoSomethingHeavy(); } } 还有一个测试: [TestFixture] public class SomeClassTest { [Test] publi
public class SomeClass {
public Model _model;
public SomeClass() {
_model = new Model();
}
public void Foo() {
_model.DoSomethingHeavy();
}
}
还有一个测试:
[TestFixture]
public class SomeClassTest {
[Test]
public void TestFooCalledSomethingHeavy() {
SomeClass someClass = NSubstitute.Substitute.For<SomeClass>();
someClass.Foo();
someClass._model.Received().DoSomethingHeavy();
}
}
[TestFixture]
公共类测试{
[测试]
public void testfoodCalledSomethingheavy(){
SomeClass SomeClass=NSubstitute.Substitute.For();
someClass.Foo();
someClass._model.Received().DoSomethingHeavy();
}
}
我正在尝试测试someClass.Foo();调用了_model.DoSomethingHeavy(),但我实际上不希望DoSomethingHeavy运行。我该怎么做?或者这是错误的方法?对于使用NSubstitute重写方法的行为,该方法需要是接口或虚拟方法的一部分 首先确保方法
DoSomethingHeavy
标记为虚拟或创建Model
类的接口(更好->提供更多的测试可能性)
public interface IModel
{
void DoSomethingHeavy();
}
public class Model : IModel
{
public void DoSomethingHeavy()
{
// heavy staff
}
}
在模型
类中实现接口
public interface IModel
{
void DoSomethingHeavy();
}
public class Model : IModel
{
public void DoSomethingHeavy()
{
// heavy staff
}
}
然后,对于测试非常重要->删除在SomeClass
public class SomeClass
{
public Model _model { get; private set; }
// Constructor will sure, that class get an instance of IModel
public SomeClass(IModel model)
{
_model = model;
}
public void Foo()
{
_model.DoSomethingHeavy();
}
}
现在,您可以创建IModel
的fake,并检查方法DoSomethingHeavy
是否在不运行实际实现的情况下执行
因为SomeClass
是测试中的类,所以您不需要模拟它-您将测试该类的行为
[Test]
public void TestFooCalledSomethingHeavy()
{
// Arrange
IModel fakeModel = Substitute.For<IModel>();
SomeClass someClass = new SomeClass(fakeModel);
// Act
someClass.Foo();
// Assert
fakeModel.Received().DoSomethingHeavy();
}
[测试]
public void testfoodCalledSomethingheavy()
{
//安排
IModel fakeModel=替换为();
SomeClass SomeClass=新的SomeClass(fakeModel);
//表演
someClass.Foo();
//断言
fakeModel.Received().DoSomethingHeavy();
}
对于使用NSubstitute重写方法的行为,该方法需要是接口或虚拟方法的一部分
首先确保方法DoSomethingHeavy
标记为虚拟或创建Model
类的接口(更好->提供更多的测试可能性)
public interface IModel
{
void DoSomethingHeavy();
}
public class Model : IModel
{
public void DoSomethingHeavy()
{
// heavy staff
}
}
在模型
类中实现接口
public interface IModel
{
void DoSomethingHeavy();
}
public class Model : IModel
{
public void DoSomethingHeavy()
{
// heavy staff
}
}
然后,对于测试非常重要->删除在SomeClass
public class SomeClass
{
public Model _model { get; private set; }
// Constructor will sure, that class get an instance of IModel
public SomeClass(IModel model)
{
_model = model;
}
public void Foo()
{
_model.DoSomethingHeavy();
}
}
现在,您可以创建IModel
的fake,并检查方法DoSomethingHeavy
是否在不运行实际实现的情况下执行
因为SomeClass
是测试中的类,所以您不需要模拟它-您将测试该类的行为
[Test]
public void TestFooCalledSomethingHeavy()
{
// Arrange
IModel fakeModel = Substitute.For<IModel>();
SomeClass someClass = new SomeClass(fakeModel);
// Act
someClass.Foo();
// Assert
fakeModel.Received().DoSomethingHeavy();
}
[测试]
public void testfoodCalledSomethingheavy()
{
//安排
IModel fakeModel=替换为();
SomeClass SomeClass=新的SomeClass(fakeModel);
//表演
someClass.Foo();
//断言
fakeModel.Received().DoSomethingHeavy();
}
您熟悉编译器指令吗。。?试试看。。或者在.Config文件中为IsTest
添加一个条目,并在运行时根据.Config键条目检查它。我不确定我是否遵循了。.Config文件会做什么?我对编译器指令不熟悉。如果有帮助的话,测试是通过Unity的测试运行程序运行的。在#if test
上进行谷歌搜索使用Unity的测试运行程序中的编译器指令在googleC#stackoverflow中进行以下搜索,并了解编译器指令是什么以及如何使用它们。。在调试模式下运行时也有一些提示,例如#if Debug
您熟悉编译器指令吗。。?试试看。。或者在.Config文件中为IsTest
添加一个条目,并在运行时根据.Config键条目检查它。我不确定我是否遵循了。.Config文件会做什么?我对编译器指令不熟悉。如果有帮助的话,测试是通过Unity的测试运行程序运行的。在#if test
上进行谷歌搜索使用Unity的测试运行程序
中的编译器指令在googleC#stackoverflow中进行以下搜索,并了解编译器指令是什么以及如何使用它们。。在调试模式下运行时也有一些方法,例如#如果调试
非常好,允许传入模型是关键,因为我能够传入模拟。非常好。非常好,允许模型通过是这里的关键,因为我能够通过模拟。效果很好。