【发布时间】:2015-06-12 21:14:42
【问题描述】:
我希望通过使用 AutoFixture 和 NSubstitue,我可以充分利用它们各自提供的功能。我自己使用 NSubstitute 取得了一些成功,但我完全不知道如何将它与 AutoFixture 结合使用。
下面的代码显示了我正在尝试完成的一组事情,但我的主要目标是完成以下场景:测试方法的功能。
- 我希望使用随机值调用构造函数(也许一个除外 - 请阅读第 2 点。)。
- 无论是在构建期间还是之后,我都想更改属性的值 -
Data。 - 接下来拨打
Execute并确认结果
我正在尝试进行的测试是:“should_run_GetCommand_with_provided_property_value”
任何帮助或参考说明如何使用 NSubstitue 和 AutFixture 的文章都会很棒。
示例代码:
using FluentAssertions;
using NSubstitute;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoNSubstitute;
using Xunit;
namespace RemotePlus.Test
{
public class SimpleTest
{
[Fact]
public void should_set_property_to_sepecified_value()
{
var sut = Substitute.For<ISimple>();
sut.Data.Returns("1,2");
sut.Data.Should().Be("1,2");
}
[Fact]
public void should_run_GetCommand_with_provided_property_value()
{
/* TODO:
* How do I create a constructor with AutoFixture and/or NSubstitute such that:
* 1. With completely random values.
* 2. With one or more values specified.
* 3. Constructor that has FileInfo as one of the objects.
*
* After creating the constructor:
* 1. Specify the value for what a property value should be - ex: sut.Data.Returns("1,2");
* 2. Call "Execute" and verify the result for "Command"
*
*/
// Arrange
var fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
// var sut = fixture.Build<Simple>().Create(); // Not sure if I need Build or Freeze
var sut = fixture.Freeze<ISimple>(); // Note: I am using a Interface here, but would like to test the Concrete class
sut.Data.Returns("1,2");
// Act
sut.Execute();
// Assert (combining multiple asserts just till I understand how to use NSubstitue and AutoFixture properly
// sut.Received().Execute();
sut.Data.Should().Be("1,2");
sut.Command.Should().Be("1,2,abc");
// Fails with : FluentAssertions.Execution.AssertionFailedExceptionExpected string to be "1,2,abc" with a length of 7, but "" has a length of 0.
}
}
public class Simple : ISimple
{
// TODO: Would like to make this private and use the static call to get an instance
public Simple(string inputFile, string data)
{
InputFile = inputFile;
Data = data;
// TODO: Would like to call execute here, but not sure how it will work with testing.
}
// TODO: Would like to make this private
public void Execute()
{
GetCommand();
// Other private methods
}
private void GetCommand()
{
Command = Data + ",abc";
}
public string InputFile { get; private set; }
public string Data { get; private set; }
public string Command { get; private set; }
// Using this, so that if I need I can easliy switch to a different concrete class
public ISimple GetNewInstance(string inputFile, string data)
{
return new Simple(inputFile, data);
}
}
public interface ISimple
{
string InputFile { get; } // TODO: Would like to use FileInfo instead, but haven't figured out how to test. Get an error of FileNot found through AutoFixture
string Data { get; }
string Command { get; }
void Execute();
}
}
【问题讨论】:
-
在构造期间或之后定义或分配特定值:stackoverflow.com/q/28350054/126014
-
@MarkSeemann 感谢您的 cmets。您的博客、书籍、代码等对我帮助很大。然而,目前我的困惑更多是如何使用 NSubstitue 和 AutoFixture 的组合。您提供的解决方案仅针对 AutoFixture。再次感谢您分享您的知识和见解。
-
你想解决什么问题?
标签: c# unit-testing autofixture nsubstitute