1

I have a course repository as follows in the Arrange

A.CallTo(
            () => _courseClientStatusRepository.GetTnCoursesForClientStatus()).Returns(new List<CourseClientStatusCreationDto>
                {
                    new CourseClientStatusCreationDto { CourseTnId = Enums.CourseLevel.Beginner  },
                    new CourseClientStatusCreationDto { CourseTnId = Enums.CourseLevel.Intermediate, },
                    new CourseClientStatusCreationDto { CourseTnId = Enums.CourseLevel.Advanced,  }
                }
            );

In the Act I have a method that calls the

void CreateClientCourseStatus(List<CourseClientStatusDto> courseClientStatusDto);

method of the ICourseClientStatusRepository

and in the Assert, I have the following. The first assert passes, the second and third asserts fail.

A.CallTo(
            () => _courseClientStatusRepository.CreateClientCourseStatus(A<List<CourseClientStatusDto>>.Ignored))
            .MustHaveHappened();

        A.CallTo(
            () => _courseClientStatusRepository.CreateClientCourseStatus(A<List<CourseClientStatusDto>>.That.Contains(A<CourseClientStatusDto>.Ignored)))
            .MustHaveHappened();


        A.CallTo(
            () => _courseClientStatusRepository.CreateClientCourseStatus(A<List<CourseClientStatusDto>>.That.Contains(A<CourseClientStatusDto>.That.Matches(
                x => x.CourseTnId == Enums.CourseLevel.Beginner               
                ))))
                .MustHaveHappened();

I am at least expecting the second assert to pass at it's expecting an instance of an object of type CourseClientStatusDto , where its specific values are not important, hence i used the .Ignored property.

Is there perhaps something that i'm doing wrong to assert if a collection contains a specific object (using fake it easy .mustHaveHappened() method) ?

Ntu
  • 41
  • 4

1 Answers1

1

@Ntu, can I ask what version of FakeItEasy you're using?

The A<T>.Ignored property is only applicable when used as a parameter specifier during call configuration or verification; it can't be used elsewhere, for example in the Contains method. As of FakeItEasy 4.1.1, you should get a clear error indicating this. For example, with FakeItEasy 4.3.0, I see this when I run an approximation of your test:

Test 'FakeItEasyQuestions2015.Ntu.NestedConstraint' failed: System.InvalidOperationException : An argument constraint, such as That, Ignored, or _, cannot be nested in an argument.

We frequently update the package, so it's always a good idea to upgrade for bug fixes and improvements.

You can replace your last two checks with something like this:

 A.CallTo(() => _courseClientStatusRepository.CreateClientCourseStatus(A<List<CourseClientStatusDto>>.That.Not.IsEmpty()))
    .MustHaveHappened();

A.CallTo(() => _courseClientStatusRepository.CreateClientCourseStatus(
A<List<CourseClientStatusDto>>.That.Matches(l => l.Exists(i => i.CourseTnId == Enums.CourseLevel.Beginner))))
    .MustHaveHappened();

While I'm commenting, I can't help but notice that in your Arrange, the return value of the A.CallTo is not used, so you won't be specifying any actual behaviour (unless you just left that out for brevity…). Consider adding the FakeItEasy.Analyzer.CSharp to your project; it'll warn you of such problems!

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • 1
    @ Blair, the last two checks are now working, your code helped, thanks. I'm using version 3.4.0 of Fake It Easy. And yes, I wanted to summarize the code in the Arrange. I definitely will be adding FakeItEasy.Analyzer.CSharp – Ntu Dec 18 '17 at 05:35