Search code examples
castle-windsorrhino-mocksmspec

Why does adding a constructor fail this MSpec test with System.InvalidOperationException?


I have this first version of a class

public class GenerateAuthorisationWorkflows : IGenerateAuthorisationWorkflows
{
    public IList<Guid> FromDtaObjects(IList<DtaObject> dtaObjects, Employee requestingEmployee)
    {
        foreach (var dtaObject in dtaObjects) { }
        return new List<Guid>();
    }

    public IList<Guid> FromDtaObjects()
    {
        return new List<Guid>();
    }
}

And the MSpec tests for it

public abstract class specification_for_generate_workflows : Specification<GenerateAuthorisationWorkflows>
{
    protected static IWorkflowService workflowService;

    Establish context = () => { workflowService = DependencyOf<IWorkflowService>(); };
}

[Subject(typeof(GenerateAuthorisationWorkflows))]
public class when_generate_workflows_is_called_with_a_dta_object_list_and_an_employee : specification_for_generate_workflows
{
    static IList<Guid> result;
    static IList<DtaObject> dtaObjects;
    static Employee requestingEmployee;

    Establish context = () =>
    {
         var mocks = new MockRepository();
         var stubDtaObject1 = mocks.Stub<DtaObject>();
         var stubDtaObject2 = mocks.Stub<DtaObject>();
         var dtaObjectEnum = new List<DtaObject>{stubDtaObject1,stubDtaObject2}.GetEnumerator();
         dtaObjects = mocks.Stub<IList<DtaObject>>();
         dtaObjects.Stub(x => x.GetEnumerator()).Return(dtaObjectEnum).WhenCalled(x => x.ReturnValue = dtaObjectEnum);
         requestingEmployee = mocks.Stub<Employee>();
         mocks.ReplayAll();
    };

    Because of = () => result = subject.FromDtaObjects(dtaObjects, requestingEmployee);

    It should_enumerate_the_dta_objects = () => dtaObjects.received(x=> x.GetEnumerator());
    It should_call_workflow_host_helper = () => workflowService.AssertWasCalled(x => x.StartWorkflow());
}

With this configuration, my first test passes and my second test fails, as expected. I added a constructor to the class to inject the IWorkflowService.

public class GenerateAuthorisationWorkflows : IGenerateAuthorisationWorkflows
{
    IWorkflowService _workflowService;

    GenerateAuthorisationWorkflows(IWorkflowService workflowService)
    {
        _workflowService = workflowService;
    }

    public IList<Guid> FromDtaObjects(IList<DtaObject> dtaObjects, Employee requestingEmployee)
    {
        foreach (var dtaObject in dtaObjects)
        {
            Guid workflowKey = _workflowService.StartWorkflow();
        }

        return new List<Guid>();
    }

    public IList<Guid> FromDtaObjects()
    {
        return new List<Guid>();
    }
}

Now, when I run the tests, they fail at the Because:

System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.First(IEnumerable`1 source)
at MSpecTests.EmployeeRequestSystem.Tasks.Workflows.when_generate_workflows_is_called_with_a_dta_object_list_and_an_employee.<.ctor>b__4() in GenerateAuthorisationWorkflowsSpecs.cs: line 76 

For clarity, line 76 above is:

Because of = () => result = subject.FromDtaObjects(dtaObjects, requestingEmployee);

I've tried tracing the problem but am having no luck. I have tried setting up a constructor that takes no arguments but it raises the same error. I have similar classes with IoC dependencies that work fine using MSpec/Rhino Mocks, where am I going wrong?


Solution

  • Castle Windsor requires a public constructor to instantiate a class. Adding public to the constructor allows correct operation.

    public class GenerateAuthorisationWorkflows : IGenerateAuthorisationWorkflows
    {
        IWorkflowService _workflowService;
    
        public GenerateAuthorisationWorkflows(IWorkflowService workflowService)
        {
            _workflowService = workflowService;
        }
    
        public IList<Guid> FromDtaObjects(IList<DtaObject> dtaObjects, Employee requestingEmployee)
        {
            foreach (var dtaObject in dtaObjects)
            {
                Guid workflowKey = _workflowService.StartWorkflow();
            }
    
            return new List<Guid>();
        }
    
        public IList<Guid> FromDtaObjects()
        {
            return new List<Guid>();
        }
    }