问题描述
我正在使用 C#、.NET Framework 4.7、Nunit 3.8.0 和 JustMock Lite 2017.2.821.1 开发 TDD 测试.
I'm developing a TDD test with C#, .NET Framework 4.7, Nunit 3.8.0 and JustMock Lite 2017.2.821.1.
当我这样做时:
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
做一些事情,我不知道如何解决这个问题.
This is the first time I do something with TDD and JustMock
and I don't know how to fix this problem.
我的测试类是:
[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
中,我得到了同样的错误.
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);
}
我认为问题出在 JustMock 上.
I think the problem is in JustMock.
推荐答案
JustMock 依赖于 NUnit 3 和 NUnit 2 的 TestFixtureSetUpAttribute 和 TestFixtureTearDownAttribute.
这两个属性在 NUnit 3.0 中已被弃用,并且刚刚在 NUnit 3.8 中被移除.JustMock 应该更新以使用它们的替代品,OneTimeSetUp
和 OneTimeTearDown
.
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
.
作为用户 - 在解决此问题之前,您不能使用高于 NUnit 3.7.1 的版本.您可以在此处向 JustMock 报告问题.
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.
此问题自 JustMock 2018 R1 发布以来已修复.
The problem is fixed since JustMock 2018 R1 release.
这篇关于在 JustMock 中找不到 TestFixtureSetUpAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!