开发者

Moq a proxy for unit testing

I'm new to using Moq and I cannot find the way for doing this. I've a generateId private 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 that reason i need to mock the Proxy. SharedServicesClientProxy is just an object that implements the interface ISharedServices but adds the singleton. I wanted to test that all the strings correctly returned a string 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";
 开发者_Python百科       actual = target.GenerateCallId();
        Assert.IsTrue(actual.StartsWith(extCallIdPrefix));
    }

I guess I'm doing my mock in the wrong place?

In a more general way, how do i mock an object that is going to be called by the method I'm testing? for 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 to moq Bar, but where do i need to do it? Thanks, Seb


Search for the term "Dependency Injection" before proceeding.

You have a dependency on SharedServicesClientProxy.Instance, which is a Singleton. This will cause problems with test isolation, since you will reuse the same instance across tests and be prone to state-leftover issues.

I'd advise to inject dependencies like this (and OT- you shouldn't be testing private methods... test via the public interface. There it is out of my system..:)

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

Rewire your current code to create the target class like this

var class = new AnonClass(SharedServicesClientProxy.Instance);

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜