2017-08-29 59 views
1

我正在用C#,.NET Framework 4.7,Nunit 3.8.0和JustMock Lite 2017.2.821.1開發TDD測試。在JustMock中找不到TestFixtureSetUpAttribute?

當我這樣做:

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

任何想法?

UPDATE

我已刪除的方法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。

+0

從異常消息看,它看起來像一個NUnit問題,因爲它說它找不到'TestFixtureSetUpAttribute'或'TestFixtureTearDownAttribute' – Nkosi

回答

5

JustMock relies on TestFixtureSetUpAttribute and TestFixtureTearDownAttribute兩個NUnit的3和NUnit 2.

這兩個屬性中的NUnit 3.0是過時,剛剛在NUnit的3.8被刪除。 JustMock應更新爲使用其替代品,OneTimeSetUpOneTimeTearDown

作爲用戶 - 在解決此問題之前,您不能使用NUnit 3.7.1之後的版本。您可以將問題報告給JustMock here

0

要進一步更新,JustMock(Version-2017.3.913.1)現在支持NUnit for 3.7.1版本,但它仍然存在3.8.x.

如果您可以靈活地降級NUnit版本,可以進行以下操作。

1:在VS中,轉到 - 工具> NuGet程序包管理器>程序包管理器控制檯。

2:PM> Install-Package NUnit -Version [lower version](只需按下-Version之後的tab鍵即可獲得所有支持的版本),您可以降級您的NUnit版本。

+0

已經很晚了。現在我只是模仿,比JustMock更容易使用。謝謝。 – VansFannel