Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Personally I've found explicitly implementing interfaces to be a world of hurtI've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}

The only option that will allow you not to change ANY of the existing code at all is to use a super-powered mocking framework like Typemock Isolator (or Moles) which can mock out just about anything, including statics.

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}

The only option that will allow you not to change ANY of the existing code at all is to use a super-powered mocking framework like Typemock Isolator (or Moles) which can mock out just about anything, including statics.

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}

The only option that will allow you not to change ANY of the existing code at all is to use a super-powered mocking framework like Typemock Isolator (or Moles) which can mock out just about anything, including statics.

added 282 characters in body
Source Link
George Mauer
  • 1.6k
  • 11
  • 13

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}

The only option that will allow you not to change ANY of the existing code at all is to use a super-powered mocking framework like Typemock Isolator (or Moles) which can mock out just about anything, including statics.

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}

The only option that will allow you not to change ANY of the existing code at all is to use a super-powered mocking framework like Typemock Isolator (or Moles) which can mock out just about anything, including statics.

added 493 characters in body
Source Link
George Mauer
  • 1.6k
  • 11
  • 13

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.

The Simplest solution (and the best one IMO when the method doesn't access or modify state):

public class HelperClass
{
    internal static Action<bool> returnSomethingImpl = () => {
      return true;
    }
    public static bool ReturnSomething() { return returnSomethingImpl(); }
}

then in your AssemblyInfo.cs

[assembly:InternalsVisibleTo('MyTestDllNameWithoutTheDll')]

and just stub it out in your test however you want:

HelperClass.returnSomethingImpl = () => {
  calls.Add("returnSomethingImpl called");
  return false;
}

If you ARE reading/writing state then a singleton would probably serve best

public class HelperClass : IHelperClass
{
    IHelperClass _instance;
    public IHelperClass Instance 
    {
      get {
        if(_instance == null) Instance = new HelperClass();
        return _instance;
      } 
      set { _instance = value }
    }    
    public static bool ReturnSomething() { return Instance.Value.ReturnSomething(); }
}
Source Link
George Mauer
  • 1.6k
  • 11
  • 13
Loading