Method mocking RRS feed

  • Question

  • User861911889 posted

    I have a code

     public int AddNewSHEDatasheet(string colour1, string colour2, string material, string title, string description, bool discontinued,string reason)
                WebSHEServiceClient _webService = new WebSHEServiceClient();
                    return _webService.AddNewSHEDatasheet(colour1, colour2, material, title, description, discontinued, reason);
                catch (Exception ex)
                    _webService = null;

    I have called the webservice to add a row into the table.

    Now I want to mock the method to use it in the test case.

    Please help me to write it step by step

    Monday, June 8, 2015 9:10 AM


  • User-271186128 posted

    Hi Sir,

    I want to mock the method to use it in the test case.

    Please help me to write it step by step

    As for this issue, here are some relevant articles about Unit testing with Mock objects, please refer to them.






    Here is a simple sample from above link, you could refer to it.

    If you are looking to mock a return value, then this is very simple. You can modify the Utility.Helperclass to include a property called OverrideLoggedInUserName. When someone calls GetLogedInUserName(), if the override property is set, it is returned, otherwise the normal code to get the value from the HttpContext is used to get the return value.

    public static class Helper
        // Set this value to override the return value of GetLoggedInUserName().
        public static string OverrideLoggedInUserName { get; set; };
        public static string GetLoggedInUserName()
            // Return mocked value if one is specified.
            if ( !string.IsNullOrEmpty( OverrideLoggedInUserName ) )
                return OverrideLoggedInUserName;
            // Normal implementation.
            string username = "";
            if ( System.Web.HttpContext.Current.User.Identity.IsAuthenticated )
                username = ( (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity ).Ticket.Name;
            return username;

    This will effectively allow you to override the return value, which technically isn't a mock--it's a stub(according to the excellent article Mocks Aren't Stubs by Martin Fowler). This allows you to stub a return value, but won't allow you to assert whether the method was called or not. Anyhow as long as you only want to manipulate the return value this works fine.

    Here is how you would use this in a test.

    [ TestMethod ]
    public void TestGetRajnis()
        // Set logged in user name to be "Bob".
        Helper.OverrideLoggedInUserName = "Bob";
        SomeController s = new SomeController( new SomeService() );
        var data = s.GetRajnis();
        // Any assertions...

    This design does have one drawback. Because it's a static class, if you set the override value, it remains set until you un-set it. So you must remember to re-set it to null.

    Best Regards,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 9, 2015 6:50 AM