1

我有第一个版本的课程

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>();
    }
}

MSpec 对其进行测试

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());
}

使用此配置,我的第一个测试通过,第二个测试失败,正如预期的那样。我在类中添加了一个构造函数来注入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>();
    }
}

现在,当我运行测试时,它们在以下位置失败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 

为清楚起见,上面的第 76 行是:

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

我试过追踪问题,但没有运气。我尝试设置一个不带参数的构造函数,但它会引发相同的错误。我有类似的具有 IoC 依赖项的类,使用 MSpec/Rhino Mocks 可以正常工作,我哪里出错了?

4

2 回答 2

1

Castle Windsor 需要一个公共构造函数来实例化一个类。添加public到构造函数允许正确操作。

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>();
    }
}
于 2012-09-13T17:12:10.967 回答
0

罗文,看起来你回答了自己的问题。明确声明访问修饰符是个好习惯!默认情况下,C# 选择private. 这些类型的错误很容易错过!

我也可以看到你的Establish块太复杂了。您正在测试实现细节而不是行为。例如,你是

  • 存根在循环GetEnumerator内隐式进行的调用。foreach
  • 断言工作流服务只被调用一次
  • 混合 MSpec 自动模拟和您自己的本地模拟

您实际上并没有测试输入列表中的每个对象是否都有一个 GUID。 如果我是你,我会测试这样的行为......

public class GenerateAuthorisationWorkflows : IGenerateAuthorisationWorkflows
{
    private readonly IWorkflowService _service;

    public GenerateAuthorisationWorkflows(IWorkflowService service)
    {
        _service = service;
    }

    public List<Guid> FromDtaObjects(List<DtaObject> input, Employee requestor)
    {
        // I assume that the workflow service generates a new key
        // per input object. So, let's pretend the call looks like
        // this. Also using some LINQ to avoid the foreach or 
        // building up a local list.
        input.Select(x => _service.StartWorkflow(requestor, x)).ToList();
    }
}

[Subject(typeof(GenerateAuthorisationWorkflows))]
public class When_generating_authorisation_keys_for_this_input 
    : Specification<GenerateAuthorisationWorkflows>
{
    private static IWorkflowService _service;
    private static Employee _requestor = new Employee();
    private static List<DtaObject> _input = new List<DtaObject>() 
    {
        new DtaObject(),
        new DtaObject(),
    };
    private static List<Guid> _expected = new List<Guid>()
    {
        Guid.NewGuid(),
        Guid.NewGuid(),
    };

    private static List<Guid> _actual = new List<Guid>();

    Establish context = () =>
    {
        // LINQ that takes each item from each list in a pair. So
        // the service is stubbed to return a specific GUID per 
        // input DtaObject.
        _input.Zip(_expected, (input, output) => 
        {
            DependencyOf<IWorkflowService>().Stub(x => x.StartWorkflow(_requestor, input)).Return(output);
        });
    };

    Because of = () => _actual = Subject.FromDtaObjects(_input, _requestor);

    // This should be an MSpec collection assertion that 
    // ensures that the contents of the collections are
    // equivalent
    It should_get_a_unique_key_per_input = _actual.ShouldEqual(_expected);
}
于 2012-12-03T21:55:29.150 回答