如何在同一单元测试中最小起订量,来自同一配置的两个值
How to moq, two values from the same config, on the same unit test
我有两个值需要在我的单元测试中使用最小起订量进行测试,但我不知道如何实现这一点。这是我目前所拥有的。当获得其中一个值时,它正在工作,但显然它会覆盖我的第二次调用。
[Test(Description = "Test writer only called once")]
public void Test_Writer_Only_Called_Once()
{
//Arrange
var SendFilesAccessMock = new Mock<ISendFiles>(MockBehavior.Strict);
var iWriterBci = new Mock<IWriter<BCIData>>(MockBehavior.Strict);
var iWriterBji = new Mock<IWriter<BJIData>>(MockBehavior.Strict);
var iConfigMock = new Mock<IConfiguration>();
**Mock<IConfigurationSection> mockSection = new Mock<IConfigurationSection>();
mockSection.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bji");
Mock<IConfiguration> mockConfig = new Mock<IConfiguration>();
mockConfig.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bjiFileLocation"))).Returns(mockSection.Object);
mockSection.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bci");
mockConfig.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bciFileLocation"))).Returns(mockSection.Object);**
ArgumentNullException exceptionThrown = null;
iWriterBji.Setup(obj => obj.Write(It.IsAny<IEnumerable<BJIData>>(), It.IsAny<StreamWriter>()));
//iConfigMock.Setup(c => c.GetSection(It.IsAny<String>())).Returns(new Mock<IConfigurationSection>().Object);
var objectToTest = _ = new Api.Business.SendFiles(iWriterBci.Object, iWriterBji.Object,mockConfig.Object);
//Act
try
{
objectToTest.Write("test", 191);
}
catch (ArgumentNullException ex)
{
exceptionThrown = ex;
}
//Assert
SendFilesAccessMock.VerifyAll();
iWriterBji.Verify(obj => obj.Write(It.IsAny<IEnumerable<BJIData>>(), It.IsAny<StreamWriter>()), Times.Once());
}
我的调用代码
public SendFiles(IWriter<BCIData> bciWriter, IWriter<BJIData> bjiWriter, IConfiguration config)
{
_bciWriter = bciWriter ?? throw new ArgumentNullException(nameof(bciWriter));
_bjiWriter = bjiWriter ?? throw new ArgumentNullException(nameof(bjiWriter));
_configuration = config ?? throw new ArgumentNullException(nameof(config));
}
public void Write(string benchName, long? orderNumber)
{
if(benchName == null) throw new ArgumentNullException(nameof(benchName));
if(orderNumber == null)
throw new ArgumentNullException(nameof(orderNumber));
try
{
var benchLocation1 = /*string.Format*/(_configuration.GetValue<string>
("AppSettings:bjiFileLocation")/*, DateTime.UtcNow.Ticks*/);
var benchLocation2 = _configuration.GetValue<string>
("AppSettings:bciFileLocation");
var passedOrderNumber = "Get BJI & BCI Data from this number";
_bjiDataList = GetBjiData();
//D:\TestBench is the location it writes to locally - need to set up folder and share
var stream1 = new StreamWriter(benchLocation1);
_bjiWriter.Write(_bjiDataList, stream1);
stream1.Close();
WriteCSVFileAndConfirm(benchLocation2);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
我似乎不知道如何设置我需要的两个配置值,因为目前,两个工作台都填充了相同的配置值。
您需要为每个配置部分创建一个单独的 Mock<IConfigurationSection>
实例:
Mock<IConfigurationSection> mockSection1 = new Mock<IConfigurationSection>();
mockSection.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bji");
Mock<IConfiguration> mockConfig = new Mock<IConfiguration>();
mockConfig
.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bjiFileLocation")))
.Returns(mockSection1.Object);
Mock<IConfigurationSection> mockSection2 = new Mock<IConfigurationSection>();
mockSection2.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bci");
mockConfig
.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bciFileLocation")))
.Returns(mockSection2.Object);
我有两个值需要在我的单元测试中使用最小起订量进行测试,但我不知道如何实现这一点。这是我目前所拥有的。当获得其中一个值时,它正在工作,但显然它会覆盖我的第二次调用。
[Test(Description = "Test writer only called once")]
public void Test_Writer_Only_Called_Once()
{
//Arrange
var SendFilesAccessMock = new Mock<ISendFiles>(MockBehavior.Strict);
var iWriterBci = new Mock<IWriter<BCIData>>(MockBehavior.Strict);
var iWriterBji = new Mock<IWriter<BJIData>>(MockBehavior.Strict);
var iConfigMock = new Mock<IConfiguration>();
**Mock<IConfigurationSection> mockSection = new Mock<IConfigurationSection>();
mockSection.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bji");
Mock<IConfiguration> mockConfig = new Mock<IConfiguration>();
mockConfig.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bjiFileLocation"))).Returns(mockSection.Object);
mockSection.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bci");
mockConfig.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bciFileLocation"))).Returns(mockSection.Object);**
ArgumentNullException exceptionThrown = null;
iWriterBji.Setup(obj => obj.Write(It.IsAny<IEnumerable<BJIData>>(), It.IsAny<StreamWriter>()));
//iConfigMock.Setup(c => c.GetSection(It.IsAny<String>())).Returns(new Mock<IConfigurationSection>().Object);
var objectToTest = _ = new Api.Business.SendFiles(iWriterBci.Object, iWriterBji.Object,mockConfig.Object);
//Act
try
{
objectToTest.Write("test", 191);
}
catch (ArgumentNullException ex)
{
exceptionThrown = ex;
}
//Assert
SendFilesAccessMock.VerifyAll();
iWriterBji.Verify(obj => obj.Write(It.IsAny<IEnumerable<BJIData>>(), It.IsAny<StreamWriter>()), Times.Once());
}
我的调用代码
public SendFiles(IWriter<BCIData> bciWriter, IWriter<BJIData> bjiWriter, IConfiguration config)
{
_bciWriter = bciWriter ?? throw new ArgumentNullException(nameof(bciWriter));
_bjiWriter = bjiWriter ?? throw new ArgumentNullException(nameof(bjiWriter));
_configuration = config ?? throw new ArgumentNullException(nameof(config));
}
public void Write(string benchName, long? orderNumber)
{
if(benchName == null) throw new ArgumentNullException(nameof(benchName));
if(orderNumber == null)
throw new ArgumentNullException(nameof(orderNumber));
try
{
var benchLocation1 = /*string.Format*/(_configuration.GetValue<string>
("AppSettings:bjiFileLocation")/*, DateTime.UtcNow.Ticks*/);
var benchLocation2 = _configuration.GetValue<string>
("AppSettings:bciFileLocation");
var passedOrderNumber = "Get BJI & BCI Data from this number";
_bjiDataList = GetBjiData();
//D:\TestBench is the location it writes to locally - need to set up folder and share
var stream1 = new StreamWriter(benchLocation1);
_bjiWriter.Write(_bjiDataList, stream1);
stream1.Close();
WriteCSVFileAndConfirm(benchLocation2);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
我似乎不知道如何设置我需要的两个配置值,因为目前,两个工作台都填充了相同的配置值。
您需要为每个配置部分创建一个单独的 Mock<IConfigurationSection>
实例:
Mock<IConfigurationSection> mockSection1 = new Mock<IConfigurationSection>();
mockSection.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bji");
Mock<IConfiguration> mockConfig = new Mock<IConfiguration>();
mockConfig
.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bjiFileLocation")))
.Returns(mockSection1.Object);
Mock<IConfigurationSection> mockSection2 = new Mock<IConfigurationSection>();
mockSection2.Setup(x => x.Value).Returns("\TestBench\TestBench2_{0}.bci");
mockConfig
.Setup(x => x.GetSection(It.Is<string>(k => k == "AppSettings:bciFileLocation")))
.Returns(mockSection2.Object);