HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Unit/integration tests

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
integrationtestsunit

Problem

Here's what I got:

[TestFixture]
public class CompositionRootTests {
    [Test]
    public void FormsShouldBeBoundToViewsThroughConventionBinding() {
        GivenIHaveANewKernel();
        WhenBindingFormsToViews();

        // Then
        kernel.Get().Should().BeOfType();
    }

    [Test]
    public void InterfacesEndingWithTheWordFactoryShouldBeBoundAsFactoriesThroughConvetionBinding() {
        GivenIHaveANewKernel();
        WhenBindingFactories();

        // Then
        kernel.Should()
            .Resolve()
            .WithSingleInstance();
    }

    private void GivenIHaveANewKernel() { kernel = new StandardKernel(); }

    private void WhenBindingFactories() {
        kernel.Bind(services => services
            .From(AppDomain.CurrentDomain
                .GetAssemblies()
                .Where(assembly => assembly.FullName.Contains("MyProject")
                                && !assembly.FullName.Contains("Test")))
            .SelectAllInterfaces()
            .EndingWith("Factory")
            .BindToFactory());
    }

    private void WhenBindingFormsToViews() {
        kernel.Bind(services => services
            .From(AppDomain.CurrentDomain
                .GetAssemblies()
                .Where(assembly => assembly.FullName.Contains("MyProject")
                                && !aasembly.FullName.Contains("Tests")))
            .SelectAllClasses()
            .EndingWith("Form")
            .BindSelection((type, baseType) => type
                .GetInterfaces()
                .Where(iface => iface.Name.EndsWith("View"))));
    }

    private IKernel kernel;
}


I'd like to write a parametirized Then method where I could pass the type to be resolved, and the type to which it should be bound.

ThenIShouldResolve();


But I wonder whether it is really cleaner than what I actually got with my // Then comment. In fact, the Given... and the When... helper methods were comments such as // Given and // When. Then, whe

Solution

Regarding your fluent approach to assertions, I'm definitely a fan of anything that makes tests more succinct and readable - as long as it's crystal clear what's going on. A one-liner like "kernel = new StandardKernel()" seems more readable than "GivenIHaveANewKernel()" to me.

As a rule of thumb the tests should be very simple without having to resort to a whole lot of syntactic gymnastics. If the tests are complicated and in need of refactoring it's often an indication that the code under test could use some refactoring itself.

Honestly a bigger issue here is what this fixture is actually testing. I would ask yourself the questions "what am I trying to test", and "do I really need to test it". In this case it appears that you are trying to test Ninject functionality rather than your own code. Time might be better off spent testing other things.

Context

StackExchange Code Review Q#62659, answer score: 6

Revisions (0)

No revisions yet.