我正在使用C#、. NET Framework 4.7,Nunit 3.8.0和JustMock Lite 2017.2.211.1开发TDD测试。

当我这样做时:

IGenericRepository<ProductionOrder> _proOrdRepository =
                Mock.Create<IGenericRepository<ProductionOrder>>();


我得到以下异常:

System.TypeInitializationException occurred
  HResult=0x80131534
  Message=An exception occurred in the type initializer of 'Telerik.JustMock.Core.Context.MockingContext'.
  Source=Telerik.JustMock
  StackTrace:
   at Telerik.JustMock.Core.Context.MockingContext.get_CurrentRepository()
   at Telerik.JustMock.Mock.<>c__44`1.<Create>b__44_0()
   at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
   at Telerik.JustMock.Mock.Create[T]()
   at MyProjects.Tests.LoadFinishedTrzlBatchTest.SetUpLoadFinishedTrzlBatch() in D:\MyProjects\MyProject\LoadFinishedTrzlBatchTest.cs:line 25

Inner Exception 1:
InvalidOperationException: Some attribute type among NUnit.Framework.TestFixtureSetUpAttribute, nunit.framework,NUnit.Framework.TestFixtureTearDownAttribute, nunit.framework not found.


这是我第一次使用TDD和JustMock进行操作,但我不知道如何解决此问题。

我的测试课是:

[TestFixture]
class LoadFinishedTrzlBatchTest
{
    private LoadFinishedTrzlBatch sut;
    private IGenericRepository<ProductionOrder> _proOrdRepository;

    [SetUp]
    public void SetUpLoadFinishedTrzlBatch()
    {
        _proOrdRepository =
            Mock.Create<IGenericRepository<ProductionOrder>>();
        var batchRepository =
            Mock.Create<IGenericRepository<Batch>>();
        var codeRepository =
            Mock.Create<IGenericRepository<Code>>();
        var aggRepository =
            Mock.Create<IGenericRepository<Aggregation>>();
        var aggChildrenRepository =
            Mock.Create<IGenericRepository<AggregationChildren>>();

        sut = new LoadFinishedTrzlBatch(
            _proOrdRepository,
            batchRepository,
            codeRepository,
            aggRepository,
            aggChildrenRepository);
    }

    [TestCaseSource(nameof(ShouldThrowArgumentSource))]
    public void ShouldThrowArgumentExceptionWithInvalidProductionOrderName(string productionOrderName)
    {
        // Assert
        Assert.That(() => sut.ExistsProductionOrder(productionOrderName), Throws.TypeOf<ArgumentNullException>());
    }

    [Test]
    public void ShouldExistsProductionOrder()
    {

        // Arrange
        var productionOrderName = "ProOrd";
        var orders = new List<ProductionOrder>() {
            new ProductionOrder { Name = productionOrderName },
            new ProductionOrder { Name = "Dummy for Filter" }
        };

        Mock.Arrange(() => _proOrdRepository
            .SearchFor(Arg.IsAny<Expression<Func<ProductionOrder, bool>>>()))
            .Returns((Expression<Func<ProductionOrder, bool>> expression) =>
                orders.Where(expression.Compile()).AsQueryable())
            .MustBeCalled();

        // Act
        var actual = sut.ExistsProductionOrder(productionOrderName);

        // Assert
        Assert.IsTrue(actual);
    }

    private static IEnumerable ShouldThrowArgumentSource()
    {
        yield return string.Empty;
        yield return null;
        yield return " ";
    }
}


任何的想法?

更新:

我删除了方法SetUpLoadFinishedTrzlBatch,并将所有内容移至方法ShouldExistsProductionOrder中,但遇到相同的错误。

[Test]
public void ShouldExistsProductionOrder()
{
    LoadFinishedTrzlBatch sut;
    IGenericRepository<ProductionOrder> _proOrdRepository;

    _proOrdRepository =
        Mock.Create<IGenericRepository<ProductionOrder>>();
    var batchRepository =
        Mock.Create<IGenericRepository<Batch>>();
    var codeRepository =
        Mock.Create<IGenericRepository<Code>>();
    var aggRepository =
        Mock.Create<IGenericRepository<Aggregation>>();
    var aggChildrenRepository =
        Mock.Create<IGenericRepository<AggregationChildren>>();

    sut = new LoadFinishedTrzlBatch(
        _proOrdRepository,
        batchRepository,
        codeRepository,
        aggRepository,
        aggChildrenRepository);

    // Arrange
    var productionOrderName = "ProOrd";
    var orders = new List<ProductionOrder>() {
        new ProductionOrder { Name = productionOrderName },
        new ProductionOrder { Name = "Dummy for Filter" }
    };

    Mock.Arrange(() => _proOrdRepository
        .SearchFor(Arg.IsAny<Expression<Func<ProductionOrder, bool>>>()))
        .Returns((Expression<Func<ProductionOrder, bool>> expression) =>
            orders.Where(expression.Compile()).AsQueryable())
        .MustBeCalled();

    // Act
    var actual = sut.ExistsProductionOrder(productionOrderName);

    // Assert
    Assert.IsTrue(actual);
}


我认为问题出在JustMock。

最佳答案

NUnit 3和NUnit 2的JustMock relies on TestFixtureSetUpAttribute and TestFixtureTearDownAttribute

这两个属性在NUnit 3.0中已弃用,而在NUnit 3.8中已被删除。 JustMock应该更新以使用其替代品OneTimeSetUpOneTimeTearDown

作为用户-在解决此问题之前,不能晚于NUnit 3.7.1使用。您可以将问题报告给JustMock here

自JustMock 2018 R1发布以来,此问题已修复。

09-28 10:02