我想定制AutoFixture的创建时行为,这样我可以在生成和分配灯具的属性后设置一些相关对象.
例如,假设我有一个自定义a的方法,User因为IsDeleted对于某组测试,它的属性总是必须为false:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsDeleted { get; set; }
}
public static ObjectBuilder<User> BuildUser(this Fixture f)
{
return f.Build<User>().With(u => u.IsDeleted, false);
}
Run Code Online (Sandbox Code Playgroud)
(我交回ObjectBuilder测试,以便在必要时可以进一步定制夹具.)
我想做的是Id在创建时自动将该用户与匿名集合相关联,但我不能这样做,因为Id当我将返回值返回到单元测试时尚未生成正确.这是我正在尝试做的事情:
public static ObjectBuilder<User> BuildUserIn(this Fixture f, UserCollection uc)
{
return f.Build<User>()
.With(u => u.IsDeleted, false);
.AfterCreation(u =>
{
var relation = f.Build<UserCollectionMembership>()
.With(ucm => ucm.UserCollectionId, …Run Code Online (Sandbox Code Playgroud) 我不相信模式,但我正在尝试创建这样的测试:我想创建Controller,但是将依赖项作为Frozen参数提供给测试.
测试如下.
[Theory, AutoNSubstituteData]
public void TestService(
[Frozen] ITestService service,
TestController controller,
string value)
{
controller.Test(value);
service.Received().ProcessValue(Arg.Any<string>());
}
Run Code Online (Sandbox Code Playgroud)
测试开始时我收到此错误.
System.InvalidOperationExceptionAn exception was thrown
while getting data for theory WebTest.Tests.Controllers.TestControllerRouteTests
.TestService:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---> System.NotImplementedException: The method or operation is not implemented.
at System.Web.HttpContextBase.get_Items()
at System.Web.WebPages.DisplayModeProvider.SetDisplayMode(HttpContextBase context, IDisplayMode displayMode)
Run Code Online (Sandbox Code Playgroud)
我已经从这个AutoNSubsituteData帖子创建了AutoNSubstituteData属性.我试图创建一个假上下文来解决问题.
/// <summary>
/// The auto n substitute data attribute.
/// </summary>
internal class AutoNSubstituteDataAttribute : AutoDataAttribute
{
/// …Run Code Online (Sandbox Code Playgroud)