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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.