2

I'm trying to implement a unit test with a methodcall assertion. (MustHaveHappened)

I'm using the following code:

[Fact]
        public void Should_set_setting_and_map_new_value_for_non_existing_setting()
        {
            //Arrange
            var userSetting = new SettingDetailsBuilder().Build();
            var respository = A.Fake<ISettingsRepository>();
            A.CallTo(() => respository.GetUserSetting(0, 0, null)).WithAnyArguments().Returns(userSetting);
            var dataretriever = new SettingsDataRetriever(respository);

            //Act
            var newUserSetting = dataretriever.SetUserSetting("variableName", "SomeOtherValue", 1, 1, "FST");

            //Assert
            A.CallTo(() => respository.GetUserSetting(1, 1, "variableName")).MustHaveHappened();
        }

But I get randomly a failed test, whereby some arguments are mentioned as "ignored". However, the assertion is with exact parameters.

Error:

Assertion failed for the following call:
    AlfaProNext.Repositories.Settings.ISettingsRepository.GetUserSetting(1, <Ignored>, <Ignored>)
  Expected to find it at least once but found it #0 times among the calls:
    1: AlfaProNext.Repositories.Settings.ISettingsRepository.Exists(varName: "variableName")
    2: AlfaProNext.Repositories.Settings.ISettingsRepository.GetUserSetting(
          userId: 1,
          profileId: 1,
          variableName: "variableName")

Does anybody knows why this is happening randomly?

Tim
  • 939
  • 1
  • 7
  • 13
  • I see you also opened FakeItEasy issue [562](https://github.com/FakeItEasy/FakeItEasy/issues/562) with this question. I've responded there with some ideas. – Blair Conrad Nov 30 '15 at 11:05

2 Answers2

2

This is likely due to tests being executed in parallel by XUnit 2.0 while using a FakeItEasy version older than 2.0.0-beta009. The latter includes a fix for issue 476, which made argument constraints using That and Ignored threadsafe.

If feasible, consider upgrading to the latest FakeItEasy. (You can see the latest changes at the GitHub Project.) Or turn off parallel test execution in XUnit.

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

After some great feedback from the FakeItEasy forum, I have my answer. Apparently the current stable version is not threadsafe and cannot handle the latest version of XUnit 2, which is running the tests in parallel.

https://github.com/FakeItEasy/FakeItEasy/issues/562

My fix was to upgrade FakeItEasy to version 2 beta 10. (alternative is to run the test in a single thread)

Tim
  • 939
  • 1
  • 7
  • 13