Checking Instance Updates in Mock

I have a method that calls a service to retrieve an instance of an object, updates the instance and saves the instance back to the service (the code for this is below).

I want to know if Moq has a good way to check the following: -

  • That the instance that is stored in the service is a modified version of the original instance
  • To have the instance updated as desired

I know that I can use It.Is<MyThing>(x => x.Name == newName)point 2 here to check. Doing this ignores point 1, though.

Is there a clean way to achieve this?

CLASS CLASS:

public class MyClass
{
    private readonly IThingService thingService;

    public MyClass(IThingService thingService)
    {
        this.thingService = thingService;
    }

    public void SaveMyThing(MyThing myThing)
    {
        var existingThing = thingService.Get(myThing.Id);
        existingThing.Name = myThing.Name;
        thingService.Save(existingThing);
    }
}

public class MyThing
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public interface IThingService
{
    MyThing Get(int id);
    void Save(MyThing myThing);
}

TEST CODE:

[Test]
public void Save_NewName_UpdatedThingIsSavedToService()
{
    // Arrange
    var myThing = new MyThing { 
                                Id = 42,
                                Name = "Thing1"
                            };
    var thingFromService = new MyThing
                    {
                        Id = 42,
                        Name = "Thing2"
                    };

    var thingService = new Mock<IThingService>();
    thingService
        .Setup(ts => ts.Get(myThing.Id))
        .Returns(thingFromService);
    thingService
        .Setup(ts => ts.Save(**UPDATED-THING-FROM-SERVICE**))
        .Verifiable();

    var myClass = new MyClass(thingService.Object);

    // Act
    myClass.SaveMyThing(myThing);

    // Assert
    thingService.Verify();
}
+3
source share
3 answers

, , , ts.Save, , .

, , Name :

thingService
     .Setup(ts => ts.Save(It.Is<MyThing>(thing => thing == thingFromService
                                               && thing.Name = myThing.Name))
     .Verifiable();
+1

:

[Test]
public void Save_NewName_UpdatedThingIsSavedToService()
{
    // Arrange
    var myThing = new MyThing { 
                                Id = 42,
                                Name = "Thing1"
                            };
    var thingFromService = new MyThing
                    {
                        Id = 42,
                        Name = "Thing2"
                    };

    var thingService = new Mock<IThingService>();
    thingService
        .Setup(ts => ts.Get(myThing.Id))
        .Returns(thingFromService)
        .Verifiable();
    thingService
        .Setup(ts => ts.Save(It.Is<MyThing>(x => x.Name == myThing.Name)))
        .Verifiable();

    var myClass = new MyClass(thingService.Object);

    // Act
    myClass.SaveMyThing(myThing);

    // Assert
    thingService.Verify();
}

, Verifiable() , It.Is, . - Save , true, Verify .

(Get and Save). Assertion Roulette, .

+1

, . , ? It.Is , MyThing, , .

, , , , , :

MyThing result = null;

thingService
    .Setup(ts => ts.Save(It.Is<MyThing>(x => x.Name == myThing.Name)))
    .Callback((MyThing saved) => result = saved)
    .Verifiable();
0

Source: https://habr.com/ru/post/1733931/


All Articles