Moq - unit testing proxy

I'm new to using Moq and I can't seem to find a way to do this. I have a private generateId method called

/// <summary>
/// Generates a call Id for external interfaces
/// </summary>
/// <returns></returns>
private string GenerateCallId()
{
return "EX" + SharedServicesClientProxy.Instance.GenerateId().ToString();
}

      

I wanted to unit test this method and for this reason I need to make fun of the proxy. SharedServicesClientProxy is just an object that implements the ISharedServices interface but adds a singleton. I wanted to check that all lines correctly returned a line starting with "EX". this is my unit test using Moq

    /// <summary>
    /// A test for GenerateCallId
    /// A CallId for external systems should always start by "EX"
    ///</summary>
    [TestMethod()]
    [DeploymentItem("myDll.dll")]
    public void GenerateCallIdTest()
    {
        myService_Accessor target = new myService_Accessor();
        var SharedServicesClientProxy = new Mock<ISharedServices>();
        SharedServicesClientProxy.Setup(x => x.GenerateId()).Returns(5396760556432785286);
        string actual;
        string extCallIdPrefix = "EX";
        actual = target.GenerateCallId();
        Assert.IsTrue(actual.StartsWith(extCallIdPrefix));
    }

      

Think I am doing my layout in the wrong place?

More generally, how do I trick an object to be called by the method I am testing? eg:

        /// <summary>
        /// dummy test
        ///</summary>
        [TestMethod()]
        [DeploymentItem("myDll.dll")]
        public void Foo()
        {
            myService_Accessor target = new myService_Accessor();
            boolexpected = false;
            actual = target.Foo();
            Assert.IsTrue(actual,expected);
        }

        /// <summary>
        /// Foo
        /// </summary>
        /// <returns></returns>
        private bool Foo()
        {
            return this.Bar();
        }

      

I need a moq bar, but where do I need it? Thanks, Seb

+2


source to share


1 answer


Look for the term "dependency injection" before continuing.

You have a dependency on SharedServicesClientProxy.Instance

which is Singleton. This will cause test isolation issues as you will be reusing the same instance in your tests and be prone to issues left in state.

I would suggest injecting dependencies like this (and OT - you shouldn't be testing private methods ... test through a public interface. There it is not on my system .. :)

public class AnonClass
{
  ISharedServices _sharedServices;
  AnonClass(ISharedServices sharedServicesObj)
  {
    _sharedServices = sharedServicesObj;
  }
  public string GenerateCallId()
  {
    return "EX" + _sharedServices.GenerateId().ToString();
  }
}

      



Repeat your current code to create a target class like this

var class = new AnonClass(SharedServicesClientProxy.Instance);

      

In your test, pass a mock object instead of the object returned by the Singleton

+6


source







All Articles