Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 639049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:47:57+00:00 2026-05-13T20:47:57+00:00

I have a method that calls a service to retrieve an instance of an

  • 0

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

What I want to know is if there is a nice way in Moq to check the following:-

  1. That the instance that is being saved back to the service is a modified version of the original instance
  2. That the instance has been updated as desired

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

Is there a clean way to achieve this?

CLASS CODE:

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:47:57+00:00Added an answer on May 13, 2026 at 8:47 pm

    If I understand correctly, you want to verify that the parameter passed to ts.Save is the instance returned from the service.

    If that’s true, check reference equality in addition to verifying that the Name value meets your expectations:

    thingService
         .Setup(ts => ts.Save(It.Is<MyThing>(thing => thing == thingFromService
                                                   && thing.Name = myThing.Name))
         .Verifiable();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a service method that calls a DAO which then returns an object
i have an Autocomplete ajax control that calls a WCF service method automatically. this
I have a WCF service method that calls a SQL stored proc. I'm developing
If I have a method that calls itself under a certain condition, is it
I have a method in controller that calls another method created in a module
I have a method song_link that calls link_to internally. I want the caller to
I have a subclass that calls a method from a superclass. The method in
I have a controller action that calls a model method which generates a serialized
Setup: I have a COM DLL that calls a method inside a managed C#
I have a UIButton which calls a method from another UIViewController , that method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.