1

I've a method with a string parameter and a bool return value. I want to ensure that it always returns true

I tried

myClass.Foo(A<string>.Ignored)
 .WithReturnType<bool>()
 .Returns(true);

Now I get the following exception

System.ArgumentException: The specified object is not recognized as a fake object. Result StackTrace: bei FakeItEasy.Core.DefaultFakeManagerAccessor.GetFakeManager(Object proxy) bei FakeItEasy.FakeFacade.GetFakeManager(Object fakedObject) bei FakeItEasy.Configuration.FakeConfigurationManager.CallTo(Object fakeObject) bei FakeItEasy.A.CallTo(Object fake) [...]

What am I doing wrong?

Boas Enkler
  • 12,264
  • 16
  • 69
  • 143

2 Answers2

4

You don't show us how you make myClass. Is it an instance of your class? It should be a fake. And the syntax is off.

Consider this example usage from the FakeItEasy tests:

var foo = A.Fake<IFoo>();
A.CallTo(() => foo.Baz(null, null)).WithAnyArguments().Returns(99);

So your example would likely end up being something like

var myClass = A.Fake<MyClass>(); // or maybe IMyClass - if Foo isn't 
                                 // virtual, you'll have problems faking it
A.CallTo((() => myClass.Foo(null)).WithAnyArguments().Returns(true);

Or () => myClass.Foo(A<string>.Ignored), but I haven't tried it.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
0

I'd suggest to go for the short form which is in its completeness (as Blair Conrad says)

var myClass = A.Fake<MyClass>(); // or maybe IMyClass - if Foo isn't 
                                 // virtual, you'll have problems faking it
A.CallTo((() => myClass.Foo(A<string>.Ignored)).Returns(true);

you can replace A<string>.Ignored by A<string>._ which are equivalent but the latter is more readable

philippdolder
  • 271
  • 2
  • 5