Parametric test with generic methods - unit-testing

In NUnit 2.5 you can do this:
[TestCase(1,5,7)]
public void TestRowTest(int i, int j, int k)
{
Assert.AreEqual(13, i+j+k);
}
You can do parametric test.
But I wonder whether you can do this or not, parametric test with generic test method? I.e.:
[TestCase <int>("Message")]
public void TestRowTestGeneric<T>(string msg)
{
Assert.AreEqual(5, ConvertStrToGenericParameter<T>(msg));
}
Or something similar.

Here is the quote from the release note of NUnit 2.5 link text
Parameterized test methods may be
generic. NUnit will deduce the correct
implementation to use based on the
types of the parameters provided.
Generic test methods are supported in
both generic and non-generic clases.
According to this, it is possible to have generic test method in non-generic class. How?
I don't quite understand Jeff's comment. In .net generics is both compile-time and run-time. We can use the reflection to find out the test case attribute associated with a method, find out the generic parameter, and again use reflection to call the generic method. It will work, no?
Update: OK, I now know how and hope it is not too late. You need the generic type to be in the parameter list. For example:
[TestCase((int)5, "5")]
[TestCase((double)2.3, "2.3")]
public void TestRowTestGeneric<T>(T value, string msg)
{
Assert.AreEqual(value, ConvertStrToGenericParameter<T>(msg));
}

You can make custom GenericTestCaseAttribute
[Test]
[GenericTestCase(typeof(MyClass) ,"Some response", TestName = "Test1")]
[GenericTestCase(typeof(MyClass1) ,"Some response", TestName = "Test2")]
public void MapWithInitTest<T>(string expectedResponse)
{
// Arrange
// Act
var response = MyClassUnderTest.MyMethod<T>();
// Assert
Assert.AreEqual(expectedResponse, response);
}
Here is implementation of GenericTestCaseAttribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class GenericTestCaseAttribute : TestCaseAttribute, ITestBuilder
{
private readonly Type _type;
public GenericTestCaseAttribute(Type type, params object[] arguments) : base(arguments)
{
_type = type;
}
IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite)
{
if (method.IsGenericMethodDefinition && _type != null)
{
var gm = method.MakeGenericMethod(_type);
return BuildFrom(gm, suite);
}
return BuildFrom(method, suite);
}
}

Create a private method and call that:
[Test]
public void TypeATest()
{
MyTest<TypeA>();
}
[Test]
public void TypeBTest()
{
MyTest<TypeB>();
}
private void MyTest<T>()
{
// do test.
}

Related

How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture?

I am using the [AutoNSubstituteData] attribute, which was posted here:
AutoFixture, xUnit.net, and Auto Mocking
I would like to combine this with the [PropertyData("")] attribute from xunit extensions.
This is my test:
public static IEnumerable<string[]> InvalidInvariant
{
get
{
yield return new string[] { null };
yield return new [] { string.Empty };
yield return new [] { " " };
}
}
[Theory, AutoNSubstituteData, PropertyData("InvalidInvariant")]
public void TestThatGuardsAreTriggeredWhenConnectionStringArgumentIsInvalid(
IDeal deal,
IDbConnection conn,
IDb db,
ISender sender,
string invalidConnString,
string query)
{
deal.Init.Group.Returns(Group.A);
deal.Aggr.Group.Returns(Group.A);
deal.Product.Commodity.Returns(Product.Commodity.E);
var sut = new Handler(db, sender);
Assert.Throws<ArgumentException>(() =>
sut.HandleDeal(deal, conn, invalidConnString, query));
}
Is there a way to combine these attributes or to get the desired functionality (mock everything, except for invalidConnstring, which should be filled with the property-data)?
There are two ways to do this:
Option 1 - Using AutoFixture.Xunit and the CompositeDataAttribute class:
internal class AutoNSubstituteDataAttribute : AutoDataAttribute
{
internal AutoNSubstituteDataAttribute()
: base(new Fixture().Customize(new AutoNSubstituteCustomization()))
{
}
}
internal class AutoNSubstitutePropertyDataAttribute : CompositeDataAttribute
{
internal AutoNSubstitutePropertyDataAttribute(string propertyName)
: base(
new DataAttribute[] {
new PropertyDataAttribute(propertyName),
new AutoNSubstituteDataAttribute() })
{
}
}
Define the test cases as below:
public class Scenario
{
public static IEnumerable<object[]> InvalidInvariantCase1
{
get
{
yield return new string[] { null };
}
}
public static IEnumerable<object[]> InvalidInvariantCase2
{
get
{
yield return new string[] { string.Empty };
}
}
public static IEnumerable<object[]> InvalidInvariantCase3
{
get
{
yield return new string[] { " " };
}
}
}
Then declare the parameterized test as:
public class Scenarios
{
[Theory]
[AutoNSubstitutePropertyData("InvalidInvariantCase1")]
[AutoNSubstitutePropertyData("InvalidInvariantCase2")]
[AutoNSubstitutePropertyData("InvalidInvariantCase3")]
public void AParameterizedTest(
string invalidConnString,
IDeal deal,
IDbConnection conn,
IDb db,
ISender sender,
string query)
{
}
}
Please note that the parameterized parameter invalidConnString have to be declared before the other parameters.
Option 2 - Using Exude:
public class Scenario
{
public void AParameterizedTest(
IDeal deal,
IDbConnection conn,
IDb db,
ISender sender,
string invalidConnString,
string query)
{
}
[FirstClassTests]
public static TestCase<Scenario>[] RunAParameterizedTest()
{
var testCases = new []
{
new
{
invalidConnString = (string)null
},
new
{
invalidConnString = string.Empty
},
new
{
invalidConnString = " "
}
};
var fixture = new Fixture()
.Customize(new AutoNSubstituteCustomization());
return testCases
.Select(tc =>
new TestCase<Scenario>(
s => s.AParameterizedTest(
fixture.Create<IDeal>(),
fixture.Create<IDbConnection>(),
fixture.Create<IDb>(),
fixture.Create<ISender>(),
tc.invalidConnString,
fixture.Create<string>())))
.ToArray();
}
}
The [Theory] attribute works by looking for one or more 'data source attributes'; for example
[InlineData]
[PropertyData]
[ClassData]
etc.
The [AutoData] attribute is just another such attribute, as is your derived [AutoNSubstituteData] attribute.
It's possible to add more than one 'data source attribute' to the same [Theory], as witnessed by the idiomatic use of the [InlineData] attribute:
[Theory]
[InlineData("foo")]
[InlineData("bar")]
[InlineData("baz")]
public void MyTest(string text)
This produces three test cases.
It's also possible to combine [PropertyData] and [AutoData], but it probably doesn't do what you want it to do. This:
[Theory]
[AutoNSubstituteData]
[PropertyData("InvalidInvariant")]
public void MyTest(/* parameters go here */)
will result in 1 + n test cases:
1 test case from [AutoNSubstituteData]
n test cases from the InvalidInvariant property
These two attributes know nothing about each other, so you can't combine them in the sense that they're aware of each other.
However, when you're implementing a property, you can write whatever code you'd like, including using a Fixture instance, so why not just do this?
public static IEnumerable<string[]> InvalidInvariant
{
get
{
var fixture = new Fixture().Customize(new MyConventions());
// use fixture to yield values...,
// using the occasional hard-coded test value
}
}
Another option is to use derive from the InlineAutoDataAttribute, which would enable you to write your test cases like this:
[Theory]
[MyInlineAutoData("foo")]
[MyInlineAutoData("bar")]
[MyInlineAutoData("baz")]
public void MyTest(string text, string someOtherText, int number, Guid id)
This would cause the first argument (text) to be populated with the constants from the attributes, while the remaining parameters are populated by AutoFixture.
Theoretically, you may also be able to combine the [AutoData] and [PropertyData] attributes using the CompositeDataAttribute, but it may not work the way you'd like.
Finally, you could consider using Exude for true first-class parameterized tests.
I have implemented an AutoPropertyDataAttribute that combines xUnit's PropertyDataAttribute with AutoFixture's AutoDataAttribute. I posted it as an answer here.
In your case you will need to inherit from the attribute in the same way as you would from an AutoDataAttribute, with the exception that you pass a fixture creation function instead of an instance:
public class AutoNSubPropertyDataAttribute : AutoPropertyDataAttribute
{
public AutoNSubPropertyDataAttribute(string propertyName)
: base(propertyName, () => new Fixture().Customize(new AutoNSubstituteCustomization()))
{
}
}

How to test two objects are equal in xunit.net?

I am reading Test Driven Development: By Example. All examples use Java and Junit (I am on chapter 10). There are one test method that test for equality of two objects. I already override Equals of the class but when run my test it failed.
This is sample code
public class BaseX
{
public string Test { get; set; }
public override bool Equals(object obj)
{
return this.Test == ((BaseX)obj).Test;
}
public override string ToString()
{
return string.Format("Tyep: {0}, Test: {1}", this.GetType().Name, this.Test);
}
}
public class A : BaseX
{
}
This is my test code
[Fact]
public void FunTest2()
{
var b1 = new BaseX();
var a1 = new A();
b1.Test = "a";
a1.Test = "a";
Assert.Equal(a1, b1);
}
When I run the test, it will failed with this message.
TDD1.UnitTest.UnitTest1.FunTest2 : Assert.Equal() Failure
Expected: Tyep: A, Test: a
Actual: Tyep: BaseX, Test: a
I think Assert.Equal compare both value and type of objects. So, I looked on xunit code and found that Assert.Equal call IEqualityComparer.Equals. If I want to compare two object with override method, what method should I use?
Update
I test this on Windows 7, Visual Studio 11 Beta, xunit.net 1.9.0.1566 (get files from nuget)
Before comparing both objects using T's Equals method, xunit compares types:
// Same type?
if (!skipTypeCheck && x.GetType() != y.GetType())
return false;
As I see it, you have two choices:
The simple choice
Assert.True(b1.Equals(a1));
It might be less expected than an Equal overload, but KISS...
The less simple choice
public class BaseXComparer : IEqualityComparer<BaseX>
{
public bool Equals(BaseX x, BaseX y)
{
return x.Test.Equals(y.Test);
}
public int GetHashCode(BaseX obj)
{
return obj.Test.GetHashCode();
}
}
And then:
Assert.Equal(a1, b1, new BaseXComparer());
In this case, consider this.
Until someone will add a new overload (shouldn't be tricky, as the inner implementation has a bool parameter for this) or an extension, I'd recommend using the simple method above.

Using RhinoMocks, how can I assert that one of several methods was called?

Consider the following service interfaces:
public interface IServiceA
{
void DoSomething(string s);
void DoSomething(string s, bool b);
}
public interface IServiceB
{
void DoSomething();
}
The implementation of IServiceB depends on IServiceA like this:
public class ServiceB : IServiceB
{
private IServiceA _serviceA;
public ServiceB(IServiceA serviceA)
{
_serviceA = serviceA;
}
public void DoSomething()
{
_serviceA.DoSomething("Hello", true);
}
}
Ie. the dependency is injected in the constructor.
Now consider a unit test for the DoSomething() method. I wish to assert that one of the overloaded DoSomething-methods in IServiceA is called, but following a general principle that unit tests shouldn't know too much about the internal workings of the method being tested, I wish to be agnostic about which of the two overloads is called. Consider the following unit test:
[TestFixture]
public class ServiceBTests
{
[Test]
public void DoSomething_CallsServiceA()
{
var serviceAMock = MockRepository.GenerateMock<IServiceA>();
var service = new ServiceB(serviceAMock);
service.DoSomething();
// Problem: How to check if EITHER:
serviceAMock.AssertWasCalled(s => s.DoSomething(Arg<String>.Is.NotNull, Arg<bool>.Is.Anything));
// OR:
serviceAMock.AssertWasCalled(s => s.DoSomething(Arg<String>.Is.NotNull));
}
}
How can I assert that either one or the other of the two methods was called?
You could manually set a boolean flag like so:
[TestFixture]
public class ServiceBTests
{
[Test]
public void DoSomething_CallsServiceA()
{
var serviceAMock = MockRepository.GenerateMock<IServiceA>();
bool called = false;
serviceAMock.Stub(
x => x.DoSomething(Arg<String>.Is.NotNull, Arg<bool>.Is.Anything))
.WhenCalled(delegate { called = true; });
serviceAMock.Stub(x => x.DoSomething(Arg<String>.Is.NotNull))
.WhenCalled(delegate { called = true; });
var service = new ServiceB(serviceAMock);
service.DoSomething();
Assert.IsTrue(called);
}
}
I don't think this very useful though. Unit tests are concerned with anything that is observable from outside of the component boundaries. Method calls to mocks are part of that. In other words, it is OK if you test for a specific overload being called. After all, there must be a reason why you use that overload and not the other one.
If you really want the unit test to remain ignorant of the implementation, you wouldn't be allowed to assert method calls on mocks at all. That would be a severe restriction on your ability to write tests.

How can I create and use a partial stub (in MoQ) without being tied to the concrete implimentation?

I have code that uses MoQ to create a partial stub. I'd prefer to interact with the interface instead of the concrete implementation so that I won't have to modify the unit test if I have a different implementation of the interface.
So for example, I have a factory method such as:
private Mock<ISomeInterface> ISomeInterfaceStubFactory()
{
return new Mock<SomeConcreteImplementation>();
}
Here is the code that calls the method:
var partialStub = ISomeInterfaceStubFactory();
partialStub.Setup(m => m.MethodToStubOutThatMethodToTestCalls(It.IsAny<string>())).Returns(new List<SomeOtherObject>());
partialStub.CallBase = true;
var actualResult= partialStub.Object.MethodToTest();
Assert.That(actualResult, Is.EqualTo(expectedResult));
The problem is that when doing this is that ISomeInterfaceStubFactory won't compile. So I changed it to be like below, but doing this seems to break the partial stub. The actual implemented MethodToStubOutThatMethodToTestCalls operation gets called, not the stubbed version. Basically I'm trying to use polymorphism with the stub object. Is there anyway to do this? I'd like my unit test to not be highly coupled to the concrete implementation.
private Mock<ISomeInterface> ISomeInterfaceStubFactory()
{
return new Mock<SomeConcreteImplementation>.As<ISomeInterface>();
}
I think you are missing the point of mock objects. Returning a mock from a concrete implementation makes no sense. The idea is to have the class under test depend on some interface or abstract which you could mock.
Revising my answer per your clarification. I don't disagree with arootbeer, but I do want to understand what you are doing and why it doesn't work.
Here's a simple example of what I think you are trying to do. The test passes for me for both concrete implementations. Is this what you are trying to do, and does this example work for you?
Interface and classes:
using System;
namespace ClassLibrary1
{
public interface IFoo
{
string GetBaseString();
string GetExtendedString();
}
public class Foo_A : IFoo
{
public virtual string GetBaseString()
{
return "Foo_A";
}
public virtual string GetExtendedString()
{
return GetBaseString() + "_Bar";
}
}
public class Foo_B : IFoo
{
public virtual string GetBaseString()
{
return "Foo_B";
}
public virtual string GetExtendedString()
{
return GetBaseString() + "_Bar";
}
}
}
Unit test:
using System;
using Xunit;
using Moq;
namespace ClassLibrary1.UnitTests
{
public class Class1
{
[Fact]
public void GetExtendedString_ReturnsExtendedString()
{
var partialFoo = IFooFactory();
partialFoo.Setup(x => x.GetBaseString()).Returns("Foo");
partialFoo.CallBase = true;
string result = partialFoo.Object.GetExtendedString();
Assert.Equal("Foo_Bar", result);
}
private Mock<IFoo> IFooFactory()
{
return new Mock<Foo_A>().As<IFoo>();
//return new Mock<Foo_B>().As<IFoo>();
}
}
}

Is this an acceptable way to test MVVM-Light Toolkit Messages?

[TestMethod()]
public void ShowSetup_SendsMessage_WhenShowSetupCommandCalled()
{
//Arrange
Messenger.Reset();
MainViewModel target = new MainViewModel();
bool wasCalled = false;
Messenger.Default.Register<NotificationMessage>(this,"Settings",(msg) => wasCalled = true);
//Act
target.ShowSetupCommand.Execute(null);
//Assert
Assert.IsTrue(wasCalled);
}
I see there is an IMessenger interface and I tried to mock it and set Messenger.OverrideDefault to the mock like this:
var mock = new Mock<IMessenger>();
Messenger.OverrideDefault((Messenger)mock.Object);
But I got an invalid cast error. Is the OverrideDefault method not for that purpose or more likely I'm using it incorrectly.
Or would I have an interface for the classes that are receiving the messages and mock those? All I really want to test is that a RelayCommand sends a message when it is called.
I just started to look at this myself. I'm a little surprised that Messenger.OverrideDefault doesn't take an IMessenger as a parameter. You have to inherit Messenger.
I suppose you could create a class that internally uses your mock object and then do a Verify.
[Test]
public void ShowSetup_SendsMessage_WhenShowSetupCommandCalled() {
Messenger.Reset();
MaintenanceViewModel target = new MainViewModel();
IMessenger mockMessenger = MockRepository.GenerateMock<IMessenger>();
mockMessenger.Expect(m => m.Send("Settings"));
TestMessenger testMessenger = new TestMessenger(mockMessenger);
Messenger.OverrideDefault(testMessenger);
bool wasCalled = false;
Messenger.Default.Register<NotificationMessage>(this, "Settings", (msg) => wasCalled = true);
target.ShowSetupCommand.Execute(null);
mockMessenger.VerifyAllExpectations();
}
You may or may not need a stub on the Register method.
The TestMessenger class:
public class TestMessenger : Messenger {
private IMessenger _mockMessenger;
public TestMessenger(IMessenger mock) {
_mockMessenger = mock;
}
public override void Register<TMessage>(object recipient, bool receiveDerivedMessagesToo, Action<TMessage> action) {
_mockMessenger.Register<TMessage>(recipient, receiveDerivedMessagesToo, action);
}
public override void Register<TMessage>(object recipient, Action<TMessage> action) {
_mockMessenger.Register<TMessage>(recipient, action);
}
public override void Send<TMessage, TTarget>(TMessage message) {
_mockMessenger.Send<TMessage, TTarget>(message);
}
public override void Send<TMessage>(TMessage message) {
_mockMessenger.Send<TMessage>(message);
}
public override void Unregister<TMessage>(object recipient, Action<TMessage> action) {
_mockMessenger.Unregister<TMessage>(recipient, action);
}
public override void Unregister<TMessage>(object recipient) {
_mockMessenger.Unregister<TMessage>(recipient);
}
public override void Unregister(object recipient) {
_mockMessenger.Unregister(recipient);
}
}
Another approach that using constructor injection you can see in this answer. I think it's better to use constructor injection instead of using static Messenger.Default. It's more robust approach cause dependency injection providing natural seam with which you can easily substitute dependencies in unit tests. If you try to substitute static member call, then you rely on internal implementation that obviously can change.