Created
September 27, 2018 14:35
-
-
Save ToniRiegler/4812345401f582755e876349978151e2 to your computer and use it in GitHub Desktop.
FakeItEasy - Faked method calls within tasks don't work all of the time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading; | |
using System.Threading.Tasks; | |
using FakeItEasy; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
public interface ITestInterface | |
{ | |
int MyMethod(); | |
} | |
[TestClass] | |
public class UnitTest1 | |
{ | |
[TestMethod] | |
public void TestMethod1() | |
{ | |
ITestInterface fake = A.Fake<ITestInterface>(); | |
int count = 0; | |
A.CallTo(() => fake.MyMethod()).ReturnsLazily(p => Interlocked.Increment(ref count)); | |
Task<int>[] tasks = | |
{ | |
Task.Run(() => fake.MyMethod()), | |
Task.Run(() => fake.MyMethod()), | |
}; | |
int[] result = Task.WhenAll(tasks).Result; | |
CollectionAssert.AreEquivalent(new[] {1, 2}, result); | |
// Assertion fails NEVER with FakeItEasy 2.3.1 | |
// Assertion fails sometimes with FakeItEasy 4.7.1 | |
// Assertion fails sometimes with FakeItEasy 4.9.0 | |
// Message: "Expected to find it twice exactly but found it once among the calls: ..." | |
A.CallTo(() => fake.MyMethod()).MustHaveHappened(Repeated.Exactly.Twice); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment