NUnit Mocking not working for Singleton Method

Feel me, I'm new to NUnit. I come from the land of Rails, so some of them are new to me.

I have a line of code that looks like this:

var code = WebSiteConfiguration.Instance.getCodeByCodeNameAndType("CATALOG_Brands_MinQty", item.Catalog);

I am trying to mock this like this (suppose it is codealready initialized):

var _websiteConfigurationMock = new DynamicMock(typeof(WebSiteConfiguration));
_websiteConfigurationMock.ExpectAndReturn("getCodeByCodeNameAndType", code);

When I debug a test, it getCodeByCodeNameAndTypereturns nullinstead of the expected one code. What am I doing wrong?

NUnit Version: 2.2.8

+2
source share
3 answers

DynamicMock creates a new object in memory representing the interface or class marshallable (inherits from MarshalByRef) that you want to make fun of.

Try the following:

var _websiteConfigurationMock = new DynamicMock(typeof(WebSiteConfiguration));
_websiteConfigurationMock.ExpectAndReturn("getCodeByCodeNameAndType", code);
WebSiteConfiguration conf = (WebSiteConfiguration)_websiteConfigurationMock.MockInstance;
var x = conf.getCodeByCodeNameAndType("CATALOG_Brands_MinQty", item.Catalog);

, , WebSiteConfiguration MarshalByRef.

, , - mock , , , , , , , , ​​ TypeMock, / .

+1

, NUnit.Mocks, NMock Moq [, , ]. , , , NUnit.Mocks .

, , , , :

. ,

// All methods you would like to mock from this class, should 
// be members of this interface
public interface IWebSiteConfiguration
{
    // Should match signature of method you are mocking
    CodeType getCodeByCodeNameAndType (
        string codeString, 
        CatalogType catalogType);
}

. ""

// You've already written the method, interface matches signature,
// should be as easy as slapping interface on class declaration
public class WebSiteConfiguration : IWebSiteConfiguration { }

.

, c. . , , [ ]. c. ctor "IWebSiteConfiguration", . ,

public class MyClass
{
    public MyClass () { }

    public void DoSomething ()
    {
        // bad singleton! bad boy! static references are bad! you
        // can't change them! convenient but bad!
        code = WebSiteConfiguration.Instance.getCodeByCodeNameAndType (
            "some.string", 
            someCatalog)
    }
}

public class MyClass
{
    private readonly IWebSiteConfiguration _config = null;

    // just so you don't break any other code, you can default
    // to your static singleton on a default ctor
    public MyClass () : this (WebSiteConfiguration.Instance) { }

    // new constructor permits you to swap in any implementation
    // including your mock!
    public MyClass (IWebSiteConfiguration config) 
    {
        _config = config;
    }

    public void DoSomething ()
    {
        // huzzah!
        code = _config.getCodeByCodeNameAndType ("some.string", someCatalog)
    }
}

unit test, , .

[Test]
public void Test ()
{
    IWebSiteConfiguration mockConfig = null;
    // setup mock instance and expectation via
    // NUnit.Mocks, NMock, or Moq

    MyClass myClass = new MyClass (mockConfig);
    myClass.DoSomething ();

    // verify results
}

Injection Dependency [DI]. "" [, -] , , [, ].

, :)

+7

, - , , , , .

: http://www.geekbeing.com/2010/05/23/how-to-unit-test-singleton-hack-in-c

?

public class TestableSingleton : SingletonClass
{
  public TestableSingleton ()
  {
    FieldInfo fieldInfo = typeof(SingletonClass)
        .GetField("_instance",
        BindingFlags.Static | BindingFlags.NonPublic);
    fieldInfo.SetValue(Instance, this);
  }
}

https://github.com/rbabreu/TestableSingleton

Visual Studio, SingletonClass . - , .

+1
source

All Articles