Tag: 嘲弄

FakeItEasy – 有一个接口伪inheritance自abstract,而两者共享相同的接口inheritance

我有一个界面 public interface IInterface { void DoSomething(); } 另一个界面 public interface IOtherInterface : IInterface { } 一个抽象类 public abstract class AbstractClass : IInterface { public void DoSomething() { Console.WriteLine(“Got here”); } } 我正在编写unit testing并伪造IOtherInterface。 抽象类已经包含了我想要用于unit testing的有用方法。 我如何制作A.Fake(); inheritance自AbstractClass ? 这是我到目前为止尝试但它不起作用 – AbstractClass.DoSomething没有被击中。 IOtherInterface fake = A.Fake(builder => builder.Implements(typeof (AbstractClass))); fake.DoSomething(); 当然,如果我做一个像这样的代理: var abstractFake = A.Fake(); […]

如何使用FakeItEasy伪造一个动作

我正在使用FakeItEasy库为我的unit testing创​​建假货。 我有一个ClassUnderTest ,我想在其上测试方法MethodToTest(Data dataObject) 。 这个方法调用一个我想伪造的接口的方法: public interface IFoo { void Execute(Action action); } public class ClassUnderTest { private IFoo _foo; public ClassUnderTest(IFoo foo) { _foo = foo; } public void MethodToTest(Data dataObject) { _foo.Execute(dataAccess => dataAccess.Update(dataObject)); } } public interface IDataAccess { void Update(Data data); } public class Data { public int Property […]

FakeItEasy代理方法调用实际实现

我正在尝试将对虚假对象的调用代理到实际实现。 这样做的原因是我希望能够使用Machine.Specifications的WasToldTo和WhenToldTo,它仅适用于接口类型的伪造。 因此,我正在执行以下操作来代理对我的真实对象的所有调用。 public static TFake Proxy(TFake fake, TInstance instance) where TInstance : TFake { fake.Configure().AnyCall().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray())); return fake; } 我会像这样使用它。 var fake = Proxy(A.Fake(), new SomeImplementation()); //in my assertions using Machine.Specifications (reason I need a fake of an interface) fake.WasToldTo(x => x.DoOperation()); 然而问题是这只适用于void方法,因为Invokes方法没有对返回值做任何事情。 (Action param代替Func) 然后我尝试使用WithReturnValue方法执行此操作。 public static TFake Proxy(TFake fake, TInstance […]

如何使用NUnit和Rhino Mocks模拟HttpContext.Current.Items

我正在使用NUnit和RhinoMocks进行(WebApi)项目的unit testing。 我正在尝试编写一个测试方法,它应该向HttpContext.Current.Items添加一个项目。 public override void OnActionExecuting(HttpActionContext actionContext) { HttpContext.Current.Items.Add(“RequestGUID”, Guid.NewGuid()); base.OnActionExecuting(actionContext); } 我不知道在测试方法中运行时如何使HttpContext.Current.Items可用于该方法。 我怎样才能做到这一点? 另外,如何检查项目是否已添加(可以/应该使用哪种断言)

在没有指定T的情况下模拟Moq中的generics方法

我有一个方法的接口如下: public interface IRepo { IA Reserve(); } 我想模拟包含此方法的类,而不必为它可用于的每种类型指定安装方法。 理想情况下,我只是希望它返回一个new mock.Object 。 我该如何实现这一目标? 看来我的解释还不清楚。 这是一个例子 – 当我指定T(这里是字符串)时,这是可能的: [TestMethod] public void ExampleTest() { var mock = new Mock(); mock.Setup(pa => pa.Reserve()).Returns(new Mock<IA>().Object); } 我想要实现的是这样的: [TestMethod] public void ExampleTest() { var mock = new Mock(); mock.Setup(pa => pa.Reserve()).Returns(new Mock<IA>().Object); // of course T doesn’t exist here. But […]