Simon Leung
asked on
ASP.NET Core Test code
What does the line below perform ?
audioServiceMock.Setup(x => x.Update(It.IsAny<Guid>(), It.Is<AudioBook>(ab => ab == audioBook))).Returns(audio Book)
TestCode02.png
audioServiceMock.Setup(x => x.Update(It.IsAny<Guid>(),
TestCode02.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so, audioServiceMock.Setup(x => x.Update(It.IsAny<Guid>(), It.Is<AudioBook>(ab => ab == audioBook))).Returns(audio Book);
can be written as
audioServiceMock.Setup(x => x.Update(It.IsAny<Guid>(), It.IsAny<AudioBook>())).Re turns(audi oBook)
correct ?
will the audioServiceMock be setup with audioBook if for the code below, as parameter ab of audioServiceMock should already be setup with ab == audioBook, correct ?
audioServiceMock.Setup(x => x.Update(It.IsAny<Guid>(), It.Is<AudioBook>(ab => ab == audioBook)))
Thx again.
can be written as
audioServiceMock.Setup(x => x.Update(It.IsAny<Guid>(),
correct ?
will the audioServiceMock be setup with audioBook if for the code below, as parameter ab of audioServiceMock should already be setup with ab == audioBook, correct ?
audioServiceMock.Setup(x => x.Update(It.IsAny<Guid>(),
Thx again.
The way it is written now... It.Is<AudioBook>(ab => ab == audioBook) it returns the audioBook object when Update is called with any Guid and an the AudioBook object audioBook.
The way @it_saige proposes It.IsAny<AudioBook>() it returns the audioBook object when Update is called with any Guid and any object of type AudioBook.
Depending on what you want to test, you must use the first or the second option. The name of the test says: Update_AudioBookPassed_Ret urnedPrope rAudioBook .... what does that mean?
- WhenUpdate_GivenAudioBookP assed_Then SameAudioB ookIsRetur ned ?
- WhenUpdate_GivenAnyAudioBo ok_ThenAud ioBookAsso ciatedWith GuidIsRetu rned ?
The way @it_saige proposes It.IsAny<AudioBook>() it returns the audioBook object when Update is called with any Guid and any object of type AudioBook.
Depending on what you want to test, you must use the first or the second option. The name of the test says: Update_AudioBookPassed_Ret
- WhenUpdate_GivenAudioBookP
- WhenUpdate_GivenAnyAudioBo
In reality, though, your mock should be setup so that it accepts any parameter(s) of the specified type and returns a known result; e.g. -
Open in new window
You verify that the parameters handled by the method are the parameters that you passed to the method (which in your attachment you already do on line 139).-saige-