TestFixtureSetUpAttribute not found in JustMock?

2019-07-13 06:54发布

问题:

I'm developing a TDD test with C#, .NET Framework 4.7, Nunit 3.8.0 and JustMock Lite 2017.2.821.1.

When I do this:

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

I get the following exception:

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.

This is the first time I do something with TDD and JustMock and I don't know how to fix this problem.

My test class is:

[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 " ";
    }
}

Any idea?

UPDATE:

I have deleted the method SetUpLoadFinishedTrzlBatch and move everything inside the method ShouldExistsProductionOrder and I get the same error.

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

I think the problem is in JustMock.

回答1:

JustMock relies on TestFixtureSetUpAttribute and TestFixtureTearDownAttribute for both NUnit 3 and NUnit 2.

These two attributes were deprecated in NUnit 3.0, and have just been removed in NUnit 3.8. JustMock should update to use their replacements, OneTimeSetUp and OneTimeTearDown.

As a user - you can't use later than NUnit 3.7.1 until this is resolved. You can report the issue to JustMock here.

The problem is fixed since JustMock 2018 R1 release.



回答2:

To update further, JustMock (Version-2017.3.913.1) is now supporting NUnit for 3.7.1 version but it still exists for 3.8.x.

In case you have the flexibility to downgrade the version of NUnit, the following can be done.

1: In VS, go to - Tools> NuGet Package Manager > Package Manager Console.

2: PM> Install-Package NUnit -Version [lower version] (Just press tab after -Version to get all supported versions) and you can downgrade your NUnit version.