Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable FakeItEasy's AutoMocking

Say I have an interface

public interface IDatabase
{
  IObjectSet<Table1> Table1 {get;}

  IObjectSet<Table2> Table2 {get;}

  //goes on to around Table400
}

So when I create an instance with FakeItEasy:

var fakeDb = A.Fake<IDatabase>();

All the properties (tables) have a default fake value. I can see why this is handy in most senarios but in mine, I need all of them to be null

Is there a clever way of doing that?

like image 672
Jens Kloster Avatar asked Jan 13 '23 07:01

Jens Kloster


2 Answers

I'm not sure it that's how it was meant to work but I found that you can set expectation on all calls to an object:

[TestMethod]
public void TestMethod1()
{
    var fake = A.Fake<IDatabase>();
    fake.AnyCall().DoesNothing();

    var result = fake.Table1;

    Assert.IsNull(result);
}

By using AnyCall you can set expectation on all of the calls to a specific fake - in this case DoesNothing returns the default value of null

like image 158
Dror Helper Avatar answered Jan 21 '23 10:01

Dror Helper


just for completeness. Another way to tell FakeItEasy it shouldn't automatically create default fake values is to declare your fake as strict like so:

var fake = A.Fake<IDatabase>(builder => builder.Strict());

though in this case FakeItEasy will throw an exception if a member is called that wasn't set up before.

In your case the suggested version is the best, though I would write it with the A syntax so you better see which calls are setups on fakes:

A.CallTo(fake).DoesNothing();

these two are identical.

like image 29
philippdolder Avatar answered Jan 21 '23 09:01

philippdolder