fakeiteasy

FakeItEasy Proxy methods calls to real implementation

末鹿安然 提交于 2020-01-01 09:36:05
问题 I'm trying to proxy calls to a fake object to the actual implementation. The reason for this is that I want to be able to use the WasToldTo and WhenToldTo of Machine.Specifications which only works on fakes of an interface type. Therefore I'm doing the following to proxy all calls to my real object. public static TFake Proxy<TFake, TInstance>(TFake fake, TInstance instance) where TInstance : TFake { fake.Configure().AnyCall().Invokes(x => x.Method.Invoke(instance, x.Arguments.ToArray()));

Using FakeItEasy, is it possible to create a dummy object of a type that takes generic type parameters

本小妞迷上赌 提交于 2019-12-23 16:28:27
问题 I have the following test: [Fact] public void StartProgram_CallsZoneProgramStart() { var zone = A.Fake<Zone>(); zone.StartProgram(); A.CallTo(() => zone.ZoneProgram.Start(null, A.Dummy<ActionBlock<InterruptInfo>>())).MustHaveHappened(Repeated.Exactly.Once); } It's creating a dummy of type ActionBlock<InterruptInfo> which is being passed into the MustHaveHappened call. zone.StartProgram definitely calles the zone.ZoneProgram.Start method, but this call is not seen by FakeItEasy. It returns the

Getting arguments passed to a FakeItEasy-mock without using magic strings?

眉间皱痕 提交于 2019-12-22 03:33:31
问题 I have been using Moq for my mocking needs the last years, but after looking at FakeItEasy i wanted to give it a try. I often want to test that a method have been called with the correct parameters, but i found no satisfactory way to do this with FakeItEasy. I have the following code to test: public class WizardStateEngine : IWizardStateEngine { private readonly IWorkflowInvoker _workflowInvoker; private List<CustomBookmark> _history; public WizardStateEngine(IWorkflowInvoker workflowInvoker)

How to test for exceptions thrown using xUnit, SubSpec and FakeItEasy

回眸只為那壹抹淺笑 提交于 2019-12-21 03:33:36
问题 I’m using xUnit, SubSpec and FakeItEasy for my unit tests. I’ve so far created some positive unit tests like the following: "Given a Options presenter" .Context(() => presenter = new OptionsPresenter(view, A<IOptionsModel>.Ignored, service)); "with the Initialize method called to retrieve the option values" .Do(() => presenter.Initialize()); "expect the view not to be null" .Observation(() => Assert.NotNull(view)); "expect the view AutoSave property to be true" .Observation(() => Assert.True

is it possible to mock/fake an extension method?

左心房为你撑大大i 提交于 2019-12-17 20:58:13
问题 I'm using a controller extension, and I tried to mock it using FakeItEasy (v 1.7.4) like this: A.CallTo(() => controller.RenderView(A<string>.Ignored,A<object>.Ignored,null)).Returns(""); but I get this error: System.NullReferenceException : Object reference not set to an instance of an object. at System.Object.GetType() at FakeItEasy.Creation.ProxyGeneratorSelector.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, ref String failReason) at FakeItEasy.Configuration

How to use FakeItEasy to assert a method was not called

▼魔方 西西 提交于 2019-12-14 02:16:47
问题 I want to assert that nothing was dispatched , a.k.a. _dispatcher.Dispatch was not called . interface being faked/mocked: interface IDispatcher { void Dispatch<T>(T command, Stuff stuff = null, TimeSpan? timeout = null, int? retries = null) where T : Command; } In the test body: _dispatcher = A.Fake<IDispatcher>(); // do stuff A.CallTo(() => _dispatcher.Dispatch(A<Command>.Ignored, A<Stuff>.Ignored, A<TimeSpan?>.Ignored, A<int?>.Ignored)).MustNotHaveHappened(); This test passes when something

Fake Captcha from commonlibnet with FakeItEasy and FluentValidation

拟墨画扇 提交于 2019-12-12 17:14:31
问题 I am using the Captcha class from commonlibrary (http://commonlibrarynet.codeplex.com/). My code works and everything but now I'm trying to write the unit test. My validation rule is: RuleFor(x => x.CaptchaUserInput) .NotEmpty() .Must((x, captchaUserInput) => Captcha.IsCorrect(captchaUserInput, x.CaptchaGeneratedText)) .WithMessage("Invalid captcha code"); In my set up code I tried to do the following: A.CallTo(() => Captcha.IsCorrect()).Returns(true); but I get the following error message:

FakeItEasy - Having an interface fake inherit from abstract while both share same interface inheritance

筅森魡賤 提交于 2019-12-12 15:11:19
问题 I have an interface public interface IInterface { void DoSomething(); } Another interface public interface IOtherInterface : IInterface { } An abstract class public abstract class AbstractClass : IInterface { public void DoSomething() { Console.WriteLine("Got here"); } } I'm writing a unit test and fake IOtherInterface. The abstract class already contains helpful methods I'd want to leverage for my unit test. How would I make my A.Fake<IOtherInterface>(); inherit from AbstractClass ? This is

How to fake an action<> with FakeItEasy

牧云@^-^@ 提交于 2019-12-12 12:23:03
问题 I'm working with the FakeItEasy library to create fakes for my unit tests. I have a ClassUnderTest on which I want to test the method MethodToTest(Data dataObject) . This method is calling a method of an interface which I want to fake: public interface IFoo { void Execute(Action<IDataAccess> action); } public class ClassUnderTest { private IFoo _foo; public ClassUnderTest(IFoo foo) { _foo = foo; } public void MethodToTest(Data dataObject) { _foo.Execute(dataAccess => dataAccess.Update

Mocking a method within a method with FakeItEasy

一笑奈何 提交于 2019-12-12 11:54:30
问题 How can I mock/facke the result from a function that is called in another function? Usually Test2 would be a DataAccess method that I dont like to fetch real data. What I like my unittest to test is the business logic. This Is what I have now but its not working at all. Sum is always asserted as 5! public int Test1() { var value = this.Test2(); //Unittest should substitute with 5 var businesslogic = value + 10; //The business logic return businesslogic; } public int Test2() { return 10; //I