From your explanation, I’m unsure what you actually want to test here. Usually, you wouldn’t test the mocked/faked object since you just configured it to behave as you want to. So if _resourceManager
is the fake, then a proper test would be one where you test a different component, that consumed _resourceManager
and called its GetString
method.
For that, a test could look like this:
// arrange
var resManager = A.Fake<IResourceManager>();
A.CallTo(() => resManager.GetString<MyTexts>(A<Expression<Func<MyTexts, string>>>.Ignored))
.Returns("Foo bar");
var testedObject = new ComponentYouWantToTest(resManager);
// act
var result = testedObject.DoSomething();
// assert
Assert.Equal("Foo bar", result.SomeProperty);
A.CallTo(() => resManager.GetString<MyTexts>(A<Expression<Func<MyTexts, string>>>.Ignored))
.MustHaveHappened(Repeated.AtLeast.Once);
Since this happens in a very specific test scenario, you very likely don’t need to care what kind of expression was passed to the GetString
method. You would only need to do that, if you expected multiple calls to the method with different expressions.
In that case though, you need to be aware that you cannot compare two expressions using ==
. So your setup fails because you compare the previously created myExpression
with a later one.
If you want to also test that your ComponentYouWantToTest
also calls the method with the correct expression, for example to check that it uses the correct property in the lambda expression, then you could do it like this:
// arrange
var myTexts = new MyTexts
{
EmailBody = "emailBody",
TheKeyAsString = "theKeyAsString",
};
var resManager = A.Fake<IResourceManager>();
A.CallTo(() => resManager.GetString<MyTexts>(A<Expression<Func<MyTexts, string>>>.Ignored))
.ReturnsLazily(((Expression<Func<MyTexts, string>>expr) => expr.Compile()(myTexts)));
var testedObject = new ComponentYouWantToTest(resManager);
// act
var result = testedObject.DoSomething();
// assert
Assert.Equal("emailBody", result.SomeProperty);
If the component used a different key function other than x => x.EmailBody
, it now wouldn’t get the emailBody
value from the myTexts
instance.