What is a Dummy used for in FakeItEasy?
Asked Answered
L

2

16

What is Dummy used for in FakeItEasy? How does it differ from A.Fake or A.Ignored ?

Thanks :-)

Loden answered 18/10, 2011 at 0:36 Comment(0)
P
14

A dummy isn't really used for anything by FakeItEasy itself, it's merely a way to create dummy instances that you can use in your tests.

For example, say that you want to test the following class:

public class Foo
{
    public void Bar(DateTime someDate);
}

Now, in one of your tests you want to invoke the bar method but the value that is passed to it is not important to the test, instead of writing:

foo.Bar(new DateTime(2000, 1, 1));

You can write:

foo.Bar(A.Dummy<DateTime>());

This signals that the value is really not important to the test so the whole reason for using it is to communicate intent better.

Puffy answered 18/10, 2011 at 13:50 Comment(3)
This still sounds like Ignored to me. What is difference in usage?Puli
Just tried this myself; using A.Dummy<> in a A.CallTo().MustHaveHappened() doesn't result in any value being accepted like A<>.Ignore does. Other than that, I don't know.Quits
I can't really see what is unclear. You do realize that the class Foo in the above is the class being tested, not a fake right? A.Dummy creates a dummy value, that's all. It can not be used when configuring calls.Pieplant
F
5

@Patrik Hägne's answer describes how users may use a Dummy, but there's another part to the story. FakeItEasy does make use of Dummies.

When FakeItEasy has to create a Fake class instance (or sometimes another Dummy class instance) by invoking one of the class's constructors, and the constructor takes arguments, it will use Dummies for the arguments.

I encourage you to check out the Dummies documentation.

Foiled answered 11/8, 2013 at 1:32 Comment(2)
So Dummy is the low level version of A<>.Ignored? The latter makes much more sense to use then.Sixtieth
Not at all. A<T>.Ignored is supplied by users to assert that a call is made. It will match any argument supplied to a call (good for when you only care about some of the other arguments). You can only use A<>.Ignore inside an assertion. I would never use a Dummy inside an assertion, as it will cause FakeItEasy to perform an equality check against the value, and if the argument it was checking ever changed in the future, the test would fail.Foiled

© 2022 - 2024 — McMap. All rights reserved.