Mr. Boy
Mr. Boy

Reputation: 63748

Does Rhino let me mock a generic method based on type?

It's easy to mock a method like string DoStuff(int input) for different values of input, with a default for all non-listed values.

But what about a method like string DoStuff<T>() for different types T?

Is this possible, if so how?

Upvotes: 1

Views: 483

Answers (1)

Old Fox
Old Fox

Reputation: 8725

Yes, you can set a behavior for generics methods like string DoStuff<T>():

public class IFoo
{
    public string DoSomthing<T>();
    public string DoSomthing<T>(T t);
}

[TestMethod]
public void TestMethod1()
{

    var fakeObj = MockRepository.GenerateStub<IFoo>();

    fakeObj.Stub(x => x.DoStuff<int>()).Return("1");
    fakeObj.Stub(x => x.DoStuff<long>()).Return("10");
    fakeObj.Stub(x => x.DoStuff<string>()).Return("XD");
    fakeObj.Stub(x => x.DoStuff(3)).Return("2");


    Assert.AreEqual("1", fakeObj.DoStuff<int>());
    Assert.AreEqual("XD", fakeObj.DoStuff<string>());
    Assert.AreEqual("10", fakeObj.DoStuff<long>());
    Assert.AreEqual("2", fakeObj.DoStuff(3));

}

Upvotes: 2

Related Questions