Testing different concrete implementations of single interface? - c#

I have the following code:
public interface IFoo
{
IResult ResolveTheProblem(IBar inputData);
}
public class FastFoo : IFoo
{
public IResult ResolveTheProblem(IBar inputData)
{
// Algorithm A - resolves the problem really fast
}
}
public class SlowFoo : IFoo
{
public IResult ResolveTheProblem(IBar inputData)
{
// Algorithm B - different algoritm, resolves the problem slow
}
}
The most important thing to test is implementation of each algorithm.
For testing I'm using NUnit and NSubstitute. Right now I have test like this:
[Test]
public void FooTest()
{
IFoo foo = Substitute.For<IFoo>();
IBar bar = Substitute.For<IBar>();
IResult result = foo.ResolveTheProblem(bar);
Assert.IsNotNull(result);
}
My two questions:
Is that test even necessary? I'm not sure about that
How can I test implementation of each IFoo (FastFoo and SlowFoo)?
EDIT: FastFoo and SlowFoo are two completely different implementations. The result of both is a random number from 1 to 10.

No, it's not necessary. Why would you want to test a substitute implementation?
You substitute your dependencies, like IBar.
You test your concrete implementations:
[Test]
public void SlowFooTest()
{
IBar bar = Substitute.For<IBar>();
// Setup bar expectations / canned responses as required
var foo = new SlowFoo(bar);
IResult result = foo.ResolveTheProblem(bar);
// Validate result from concrete class:
Assert.IsNotNull(result);
}
[Test]
public void FastFooTest()
{
IBar bar = Substitute.For<IBar>();
var foo = new FastFoo(bar);
IResult result = foo.ResolveTheProblem(bar);
Assert.IsNotNull(result);
}

Is that test even necessary? I'm not sure about that
That test does not appear to do anything. You appear to be creating a test double for the service under test.
How can I test implementation of each IFoo (FastFoo and SlowFoo)?
Is the answer always going to be the same for FastFoo and SlowFoo, or does FastFoo trade accuracy for speed?
If they are always the same then inheritance. Create a base FooTest with an abstract CreateFoo. Then two concrete implementations.
If they are not always the same, then again inheritance but with a fuzzy element.
abstract class AbstractFooTester {
[Test]
public void WhenBarIsSomethingThenResultIsSomethingElse() {
var mockRandomNumberGenerator = createRandomNumberMock(5);
var mockBar = Substitute.For<IBar>();
// set up Bar
...
var subject = createFoo(mockRandomNumberGenerator);
IResult result = subject.ResolveTheProblem(bar);
AssertResult(result, ...);
}
abstract Foo createFoo(RandomNumberGenertor g);
RandomNumberGenertor createRandomNumberMock(Int i) { ... }
}
class TestFastFoo extends AbstractFooTester {
Foo createFoo(RandomNumberGenertor g) { return new FastFoo(g); }
}
class TestSlowFoo extends AbstractFooTester {
Foo createFoo(RandomNumberGenertor g) { return new SlowFoo(g); }
}

Related

Unit test with multiple interfaces

I have Web API project with the following service class called from API Controller. I want to write unit testcase for the below class using Moq framework. How can I construct multiple interfaces using Moq? If its not possible using Moq, is there any other framework?
public class MyService : IMyService
{
private readonly IInterface1 _interface1;
private readonly IInterfaces2 _interface2;
private readonly IInterface3 _interface3;
public MyService(IInterface1 interface1,IInterface2 interface2,IInterface3 interface3)
{
_interface1=interface1;
_interface2=interface2;
_interface3=interface3;
}
public SomeModel MyMethod1(1Model model)
{
//do something here....
}
public SomeMode2 MyMethod2(Model2 model)
{
//do something here....
}
public SomeMode3 MyMethod3(Model3 model)
{
//do something here....
}
}
Imagine you have these interfaces:
public interface IOne
{
int Foo();
}
public interface ITwo
{
int Foo(string str);
}
And you have a class which depends on above interfaces:
public class Some
{
private readonly IOne one;
private readonly ITwo two;
public Some(IOne one, ITwo two)
{
this.one = one;
this.two = two;
}
public void Work()
{
// Uses one and two
}
}
And now you want to test the Work() method and you want to mock the dependencies, here is how:
// Arrange
// Let's set up a mock for IOne so when Foo is called, it will return 5
var iOneMock = new Mock<IOne>();
iOneMock.Setup(x => x.Foo()).Returns(5);
// Let's set up the mock for ITwo when Foo is called with any string,
// it will return 1
var iTwoMock = new Mock<ITwo>();
iTwoMock.Setup(x => x.Foo(It.IsAny<string>())).Returns(1);
var some = new Some(iOneMock.Object, iTwoMock.Object);
// Act
some.Work();
// Assert
// Let's verify iOneMock.Foo was called.
iOneMock.Verify(x => x.Foo());
// Let's verify iTwoMock.Foo was called with string "One" and was called only once
iTwoMock.Verify(x => x.Foo("One"), Times.Once());
In my example above I tried to show methods which take an argument, methods which take no argument, verifying method was called and verify method was called once. That should give you and idea of the options available. There are many other options available. Please see the Moq documentation for more.
You can use AutoMoq to solve the dependency injections.
var mocker = new AutoMoqer();
var myService = mocker.Create<MyService>();
var interface1 = mocker.GetMock<IInterface1>();

Mocking a method on a property

I need to set the return value for a method returned by a property, basically I need to set what this does:
mockedObject.TheProperty.GetTheValues()
I just need it to return Enumerable.Empty<MyType>.
For the purposes of demonstrating that the functionality exists assuming
public interface IFoo {
IBar TheProperty { get; set; }
}
public interface IBar {
IEnumerable<MyType> GetTheValues();
}
public class MyType { }
Moq allows for auto mocking hierarchies otherwise known as recursive mocks
[TestClass]
public class RecursiveMocksTests {
[TestMethod]
public void Foo_Should_Recursive_Mock() {
//Arrange
IEnumerable<MyType> expected = Enumerable.Empty<MyType>();
var mock = new Mock<IFoo>();
// auto-mocking hierarchies (a.k.a. recursive mocks)
mock.Setup(_ => _.TheProperty.GetTheValues()).Returns(expected);
var mockedObject = mock.Object;
//Act
IEnumerable<MyType> actual = mockedObject.TheProperty.GetTheValues();
//Assert
actual.Should().BeEquivalentTo(expected);
}
}
Note that at no point was IBar ever initialized or configured. The framework will auto mock that interface because of the setup shown above.
If however, more functionality is needed from an IBar, then a proper mock should be done and configured accordingly. There is also nothing stopping the use of configuring multiple IBar members via the IFoo mock.
Reference Moq Quickstart: Properties
Imagine you have this:
public interface IA
{
IEnumerable<MyType> TheProperty { get; set; }
}
public class MyType {}
Then here is how to mock it so when TheProperty is called, it returns and IEnumerable.Empty<MyType>:
[TestMethod]
public void SomeTest()
{
/* Arrange */
var iAMock = new Mock<IA>();
iAMock.Setup(x => x.TheProperty).Returns(Enumerable.Empty<MyType>());
/* Act */
/* Assert */
}

c# mock return base method

I am writing a unit test case with NUnit framework to test our code.
The code has referenced to 3rd party libraries like below:
class MyClass: BaseClass
{
public void override FunctionA()
{
var a = BaseFunctionB();
}
}
we don't have sourcecode for BaseClass, but the BaseFunctionB is non-virtual.
I was trying to
Setup(x=> x.BaseFunctionB()).Reteruns("my values");
but it doesn't allow.
I just want to test the FunctionA in MyClass, I don't care whether it's correct in BasefunctionB.
How to test in this case?
----------------------------2018-01-03 updated---------------------------------
I made some update for the BaseClass:
public abstract class BaseClass1//we dont have source code for this class
{
public int GetValue()
{
//do something here
return 1;//
}
public abstract int GenerateOutPut();
}
class abstract class BaseClass2: BaseClass1
{
public new virtual int GetValue()
{
return base.GetValue();
}
}
class MyClass1: BaseClass2
{
public override int GenerateOutPut()
{
var a = GetValue();
a += 1;
return a;
}
}
class MyClass2: BaseClass2
{
public override int GenerateOutPut()
{
var a = GetValue();
a -= 1;
return a;
}
}
// there are many MyClass
class MyClassN: BaseClass2
{
public override int GenerateOutPut()
{
var a = GetValue();
//different logic here.
return a;
}
}
i made a class for testing MyClass1 like below:
class TestClass1: MyClass1
{
public override int GetValue()
{
return 100;
}
}
test case as below:
public void TestFunction()
{
var test = new TestClass1();
var result = test.GetValue();
assert.AreEqual(101, result);
}
Now I have to create many TestClas which looks not good. but in terms of running out code coverage, i have to make it( i try to use mock object to execute, there is no code covered in report, i guess because it create proxy and run it on proxy, so i create the same thing myself to test the original source code)
Do i have a better solution?
Creating the second base class and the new member to encapsulate the 3rd party dependency was a good idea. It allows you to override the member in derived classes. In general try to avoid mocking what you do not own. Instead encapsulate 3rd party dependencies behind an abstraction you control so as to allow you the flexibility to mock/stub/fake any desired behavior for testing.
Using MyClass1 from your example
public class MyClass1 : BaseClass2 {
public override int GenerateOutPut() {
var a = GetValue();
a += 1;
return a;
}
}
The following test can be done to verify the expected behavior from the subject under test. Note Moq allows for base members to be called by setting CallBase = true on the mocked object.
[TestClass]
public class MyClass1_Test {
[TestMethod]
public void MyClass1_Should_Generate_Output() {
//Arrange
var expected = 0;
var mock = new Mock<MyClass1>() {
CallBase = true //<-- let mock call base members
};
mock.Setup(_ => _.GetValue()).Returns(expected); // <-- mocked behavior
var sut = mock.Object; //<-- subject under test.
//Act
var actual = sut.GenerateOutPut();
//Assert
actual.Should().Be(expected + 1);
}
}
Which is almost like what you did manually but now via the mock proxy.

Practical advantage of generics vs interfaces

What would be a practical advantage of using generics vs interfaces in this case:
void MyMethod(IFoo f)
{
}
void MyMethod<T>(T f) : where T : IFoo
{
}
I.e. what can you do in MyMethod<T> that you couldn't in the non-generic version? I'm looking for a practical example, I know what the theoretical differences are.
I know that in MyMethod<T>, T will be the concrete type, but nonetheless I will only be able to use it as an IFoo within the body of the method. So what would be a real advantage?
Calling a method through an interface is slower than calling it directly on the concrete type
If the type implementing IFoo is a value type, the non-generic version will box the value of the parameter, and boxing can negatively affect performance (especially if you call this method very often)
If your method returns a value, the generic version can return a T rather than a IFoo, which is convenient if you need to call a method of T on the result
Well, one advantage as mentioned elsewhere, would be the ability to return a specific type of IFoo type if you return a value. But since your question is specifically about void MyMethod(IFoo f), I wanted to give a realistic example of at least one type of situation where using a generic method makes more sense (to me) than the interface. (Yes I spent a bit of time on this, but I wanted to try out some different ideas. :D)
There are two blocks of code, the first is just the generic method itself and some context, the second is the full code for the example, including lots of comments ranging from notes on possible differences between this and an equivalent non-generic implementation, as well as various things I tried while implementing that didn't work, and notes on various choices I made, etc. TL;DR and all that.
Concept
public class FooChains : Dictionary<IFoo, IEnumerable<IFoo>> { }
// to manage our foos and their chains. very important foo chains.
public class FooManager
{
private FooChains myChainList = new FooChains();
// void MyMethod<T>(T f) where T : IFoo
void CopyAndChainFoo<TFoo>(TFoo fromFoo) where TFoo : IFoo
{
TFoo toFoo;
try {
// create a foo from the same type of foo
toFoo = (TFoo)fromFoo.MakeTyped<TFoo>(EFooOpts.ForChain);
}
catch (Exception Ex) {
// hey! that wasn't the same type of foo!
throw new FooChainTypeMismatch(typeof(TFoo), fromFoo, Ex);
}
// a list of a specific type of foos chained to fromFoo
List<TFoo> typedFoos;
if (!myChainList.Keys.Contains(fromFoo))
{
// no foos there! make a list and connect them to fromFoo
typedChain = new List<TFoo>();
myChainList.Add(fromFoo, (IEnumerable<IFoo>)typedChain);
}
else
// oh good, the chain exists, phew!
typedChain = (List<TFoo>)myChainList[fromFoo];
// add the new foo to the connected chain of foos
typedChain.Add(toFoo);
// and we're done!
}
}
Gory Details
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IFooedYouOnce
{
// IFoo
//
// It's personality is so magnetic, it's erased hard drives.
// It can debug other code... by actually debugging other code.
// It can speak Haskell... in C.
//
// It *is* the most interesting interface in the world.
public interface IFoo
{
// didn't end up using this but it's still there because some
// of the supporting derived classes look silly without it.
bool CanChain { get; }
string FooIdentifier { get; }
// would like to place constraints on this in derived methods
// to ensure type safety, but had to use exceptions instead.
// Liskov yada yada yada...
IFoo MakeTyped<TFoo>(EFooOpts fooOpts);
}
// using IEnumerable<IFoo> here to take advantage of covariance;
// we can have lists of derived foos and just cast back and
// forth for adding or if we need to use the derived interfaces.
// made it into a separate class because probably there will be
// specific operations you can do on the chain collection as a
// whole so this way there's a spot for it instead of, say,
// implementing it all in the FooManager
public class FooChains : Dictionary<IFoo, IEnumerable<IFoo>> { }
// manages the foos. very highly important foos.
public class FooManager
{
private FooChains myChainList = new FooChains();
// would perhaps add a new() constraint here to make the
// creation a little easier; could drop the whole MakeTyped
// method. but was trying to stick with the interface from
// the question.
void CopyAndChainFoo<TFoo>(TFoo fromFoo) where TFoo : IFoo
// void MyMethod<T>(T f) where T : IFoo
{
TFoo toFoo;
// without generics, I would probably create a factory
// method on one of the base classes that could return
// any type, and pass in a type. other ways are possible,
// for instance, having a method which took two IFoos,
// fromFoo and toFoo, and handling the Copy elsewhere.
// could have bypassed this try/catch altogether because
// MakeTyped functions throw if the types are not equal,
// but wanted to make it explicit here. also, this gives
// a more descriptive error which, in general, I prefer
try
{
// MakeTyped<TFoo> was a solution to allowing each TFoo
// to be in charge of creating its own objects
toFoo =
(TFoo)fromFoo.MakeTyped<TFoo>(EFooOpts.ForChain);
}
catch (Exception Ex) {
// tried to eliminate the need for this try/catch, but
// didn't manage. can't constrain the derived classes'
// MakeTyped functions on their own types, and didn't
// want to change the constraints to new() as mentioned
throw
new FooChainTypeMismatch(typeof(TFoo), fromFoo, Ex);
}
// a list of specific type foos to hold the chain
List<TFoo> typedFoos;
if (!myChainList.Keys.Contains(fromFoo))
{
// we just create a new one and link it to the fromFoo
// if none already exists
typedFoos = new List<TFoo>();
myChainList.Add(fromFoo, (IEnumerable<IFoo>)typedFoos);
}
else
// otherwise get the existing one; we are using the
// IEnumerable to hold actual List<TFoos> so we can just
// cast here.
typedFoos = (List<TFoo>)myChainList[fromFoo];
// add it in!
typedFoos.Add(toFoo);
}
}
[Flags]
public enum EFooOpts
{
ForChain = 0x01,
FullDup = 0x02,
RawCopy = 0x04,
Specialize = 0x08
}
// base class, originally so we could have the chainable/
// non chainable distinction but that turned out to be
// fairly pointless since I didn't use it. so, just left
// it like it was anyway so I didn't have to rework all
// the classes again.
public abstract class FooBase : IFoo
{
public string FooIdentifier { get; protected set; }
public abstract bool CanChain { get; }
public abstract IFoo MakeTyped<TFoo>(EFooOpts parOpts);
}
public abstract class NonChainableFoo : FooBase
{
public override bool CanChain { get { return false; } }
}
public abstract class ChainableFoo : FooBase
{
public override bool CanChain { get { return true; } }
}
// not much more interesting to see here; the MakeTyped would
// have been nicer not to exist, but that would have required
// a new() constraint on the chains function.
//
// or would have added "where TFoo : MarkIFoo" type constraint
// on the derived classes' implementation of it, but that's not
// allowed due to the fact that the constraints have to derive
// from the base method, which had to exist on the abstract
// classes to implement IFoo.
public class MarkIFoo : NonChainableFoo
{
public MarkIFoo()
{ FooIdentifier = "MI_-" + Guid.NewGuid().ToString(); }
public override IFoo MakeTyped<TFoo>(EFooOpts fooOpts)
{
if (typeof(TFoo) != typeof(MarkIFoo))
throw new FooCopyTypeMismatch(typeof(TFoo), this, null);
return new MarkIFoo(this, fooOpts);
}
private MarkIFoo(MarkIFoo fromFoo, EFooOpts parOpts) :
this() { /* copy MarkOne foo here */ }
}
public class MarkIIFoo : ChainableFoo
{
public MarkIIFoo()
{ FooIdentifier = "MII-" + Guid.NewGuid().ToString(); }
public override IFoo MakeTyped<TFoo>(EFooOpts fooOpts)
{
if (typeof(TFoo) != typeof(MarkIIFoo))
throw new FooCopyTypeMismatch(typeof(TFoo), this, null);
return new MarkIIFoo(this, fooOpts);
}
private MarkIIFoo(MarkIIFoo fromFoo, EFooOpts parOpts) :
this() { /* copy MarkTwo foo here */ }
}
// yep, really, that's about all.
public class FooException : Exception
{
public Tuple<string, object>[] itemDetail { get; private set; }
public FooException(
string message, Exception inner,
params Tuple<string, object>[] parItemDetail
) : base(message, inner)
{
itemDetail = parItemDetail;
}
public FooException(
string msg, object srcItem, object destType, Exception inner
) : this(msg, inner,
Tuple.Create("src", srcItem), Tuple.Create("dtype", destType)
) { }
}
public class FooCopyTypeMismatch : FooException
{
public FooCopyTypeMismatch(
Type reqDestType, IFoo reqFromFoo, Exception inner
) : base("copy type mismatch", reqFromFoo, reqDestType, inner)
{ }
}
public class FooChainTypeMismatch : FooException
{
public FooChainTypeMismatch(
Type reqDestType, IFoo reqFromFoo, Exception inner
) : base("chain type mismatch", reqFromFoo, reqDestType, inner)
{ }
}
}
// I(Foo) shot J.R.!
Doing things like these is easier:
void MyMethod<T>(T f) where T : IFoo, new() {
var t1 = new T();
var t2 = default(T);
// Etc...
}
Also, as you introduce more interfaces, generics may be more "gentle" to callers. For example, you can inherit a class from 2 interfaces and pass it directly, like this...
interface IFoo {
}
interface IBar {
}
class FooBar : IFoo, IBar {
}
void MyMethod<T>(T f) where T : IFoo, IBar {
}
void Test() {
FooBar fb = new FooBar();
MyMethod(fb);
}
...while "interface-only" method would require an "intermediary" interface (IFooBar)...
interface IFoo {
}
interface IBar {
}
interface IFooBar : IFoo, IBar {
}
class FooBar : IFooBar {
}
void MyMethod(IFooBar f) {
}
void Test() {
FooBar fb = new FooBar();
MyMethod(fb);
}
2 years later I found a very simple and useful case. Consider this common pattern:
class MyClass : IDisposable {
public void Dispose() {
if (m_field1 != null) {
m_field1.Dispose();
m_field1 = null;
}
if (m_field2 != null) {
m_field2.Dispose();
m_field2 = null;
}
// etc
}
}
I've always wanted to write a helper method to avoid having to write all this boilerplate for every field:
class MyClass : IDisposable {
static void IfNotNullDispose(ref IDisposable disposable) {
if (disposable != null) {
disposable.Dispose();
disposable = null;
}
}
public void Dispose() {
IfNotNullDispose(ref m_field1);
IfNotNullDispose(ref m_field2);
// etc
}
}
Unfortunately this is illegal in C# because you cannot use an interface for ref parameters, you must use the concrete type you'll pass in and nothing else. So you'd have to write a different method for every single type of field you want to dispose. Oh wait that's exactly what generics do for you:
static void IfNotNullDispose<T>(ref T disposable) where T: class, IDisposable {
if (disposable != null) {
disposable.Dispose();
disposable = null;
}
}
Now everything works as intended!
In this particular case, there is no benefit. In general you wouldn't specify this at a method level, but at a class level. E.g.,
public interface IFoo {
void DoSomethingImportant();
}
public class MyContainer<T> where T : IFoo {
public void Add(T something){
something.DoSomethingImportant();
AddThisThing(something);
}
public T Get() {
T theThing = GetSomeKindOfThing();
return theThing;
}
}
Notice that we require T to implement IFoo because of the Add method where we need to call the DoSomethingImportantMethod implemented by IFoo.
But notice in the Get method that we will return the T provided by end user of this class instead of a plain old IFoo, which alleviates the need for the developer to always cast to their actual concrete T.
Example:
public class Bar : IFoo{
//....
}
MyContainer<Bar> m = new MyContainer<Bar>();
//stuff happens here
Bar b = m.Get();
Note that if I was just returning an IFoo, then I would have to do this at the last line instead:
Bar b = (Bar) m.Get();
The interface method will supply you an f of type IFoo, whereas the generic version will supply you a type T with the constraint that T has to implement IFoo.
The second method would allow you to have some kind of lookup depending on T, as you have a concrete type to work with.
referring to the benchmark reported above
#Branko, calling a method through an interface is actually slower than >a "normal" virtual method call... Here's a simple benchmark: >pastebin.com/jx3W5zWb – Thomas Levesque Aug 29 '11 at 0:33
running the code in Visual Studio 2015 the result are roughly equivalent between Direct call and Through interface:
Direct call: 90,51 millisec; 112,49 millisec; 81,22 millisec
Through interface: 92,85 millisec;90,14 millisec; 88,56 millisec
the code used to benchmark (from http://pastebin.com/jx3W5zWb ) is:
using System;
using System.Diagnostics;
namespace test
{
class MainApp
{
static void Main()
{
Foo f = new Foo();
IFoo f2 = f;
// JIT warm-up
f.Bar();
f2.Bar();
int N = 10000000;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < N; i++)
{
f2.Bar();
}
sw.Stop();
Console.WriteLine("Through interface: {0:F2}", sw.Elapsed.TotalMilliseconds);
sw.Reset();
sw.Start();
for (int i = 0; i < N; i++)
{
f.Bar();
}
sw.Stop();
Console.WriteLine("Direct call: {0:F2}", sw.Elapsed.TotalMilliseconds);
Console.Read();
}
interface IFoo
{
void Bar();
}
class Foo : IFoo
{
public virtual void Bar()
{
}
}
}
}
The generic version allows you to use any type as T - which you for some reason restricted back by using the where clause, whereas your non-generic version supports only something implementing IFoo.
Another (maybe better) question is - are these two options equivalent?

Why do Rhino.Mocks and Moq say that Bar is a non-overridable member?

Could someone explain why both tests using the latest versions of Moq and Rhino.Mocks frameworks fail complaining that Bar is not a virtual/overridable method:
public interface IFoo
{
string Bar();
}
public class Foo : IFoo
{
public string Bar()
{
return "Bar";
}
}
[TestMethod]
public void MoqTest()
{
var foo = new Mock<Foo>();
foo.Setup(f => f.Bar()).Returns("abc");
Assert.AreEqual("abc", foo.Object.Bar());
}
[TestMethod]
public void RhinoTest()
{
var foo = new MockRepository().PartialMock<Foo>();
foo.Expect(f => f.Bar()).Return("abc");
foo.Replay();
Assert.AreEqual("abc", foo.Bar());
}
If I declare Bar method as virtual both tests pass. I don't understand why I have to declare Bar as virtual. Isn't it already virtual? It comes from the interface.
Virtual is part of the class not the interface, so if you wish to override the method on the class Foo you will need to declare it as virtual.
However as Krzysztof mentions if all you need are the methods on the interface IFoo then you should mock the interface.
Because you're mocking Foo class.
Mock IFoo intefrace instead
var foo = new MockRepository().PartialMock<IFoo>();

Categories

Resources