Reputation: 2579
With FakeItEasy, how to assert, that any of calls has happened?
The use case is I'm writing a class that works with a repository and as a result of a method, the class should remove some elements from the repository, either by calling DeleteAll, or by calling Delete for all of them.
For now, I've used a try-catch like this:
try // either
{
A.CallTo(() => module.CardRepo.Delete(A<CardData>.That.IsEqualTo(dummy.CardData[0]))).MustHaveHappened();
A.CallTo(() => module.CardRepo.Delete(A<CardData>.That.IsEqualTo(dummy.CardData[1]))).MustHaveHappened();
}
catch (ExpectationException) // or
{
A.CallTo(() => module.CardRepo.DeleteAll(A<IEnumerable<CardData>>.That.Contains(dummy.CardData[0]))).MustHaveHappened();
A.CallTo(() => module.CardRepo.DeleteAll(A<IEnumerable<CardData>>.That.Contains(dummy.CardData[1]))).MustHaveHappened();
}
but I don't like that and for more choices it would quickly become really ugly. Is there a better way? I couldn't find anything on FakeItEasy wiki.
Upvotes: 0
Views: 1371
Reputation: 11586
Here's an example of how you can verify if one of more methods were called on a fake object.
public interface IBlah
{
void Delete(int x);
int Add(int x, int y);
int Subtract(int x, int y);
}
public class Something
{
private readonly IBlah _blah;
public Something(IBlah blah) { _blah = blah; }
public void DoSomething(int x, int y )
{
// _blah.Add(x, y);
_blah.Subtract(x, y);
}
}
Above is just a trivial system to test
[Fact]
public void TestFeature()
{
var fake = A.Fake<IBlah>();
var something = new Something(fake);
something.DoSomething(1, 2);
var callResults = Fake.GetCalls(fake).Any(call =>
{
var x = call.GetArgument<int>("x");
var y = call.GetArgument<int>("y");
return call.Method.Name == "Add" || call.Method.Name == "Subtract"
&& x == 1 && y == 2;
});
Assert.True(callResults);
}
Above immediate snippet above is a XUnit test that verifies if either Add or Subtract were called with the giving values for the parameters x =1, y = 2
Upvotes: 2
Reputation: 569
Not sure if you can do this with FakeItEasy to be honest, you could probably write an extension method to make your code look 'nicer', although I agree it's not a great solution in general.
What I really wanted to say was that through unit teesting you're testing deterministic behavior, so through the input to the method shouldn't you know whether a call to Delete or DeleteAll should be made? The point being shouldn't these be split out into different tests? Maybe I don't have enough info about your case here.
Upvotes: 1