我正在尝试验证我的基础架构。AutoFixture 说我的代码中有一个循环引用,当我省略该行为时,它无法将样本转换为我的对象类型。
绘制解决方案依赖关系图不会产生循环引用。autofixture 的输出似乎给了我一个域对象指向域对象的路径。我不明白……它们很简单。
我会继续前进,因为我有一个通过的测试,但我真的很想确保我没有不需要的循环引用。我还担心,如果在此示例中没有构建我的简单域对象,我以后将无法将 AutoFixture 用于隔离组件。
这是成功的测试:
[Fact]
[UseDataService(typeof(DatabaseVersionService))]
public void DataServiceGetsDatabaseVersionNonAutoFixture()
{
var sut = new VersionClientProxy(new Uri(ConfigurationManager.AppSettings["DataServiceHostAddress"]));
var expected = new DatabaseVersion()
{
MajorVersionId = 0,
MinorVersionId = 0,
BuildVersionId = 0,
VersionId = 1
};
var actual = (from a in sut.DatabaseVersions
orderby a.VersionId descending
select a).FirstOrDefault();
Assert.Equal(expected.MajorVersionId, actual.MajorVersionId);
Assert.Equal(expected.MinorVersionId, actual.MinorVersionId);
Assert.Equal(expected.BuildVersionId, actual.BuildVersionId);
Assert.Equal(expected.VersionId, actual.VersionId);
}
这是失败的测试:
[Fact]
[UseDataService(typeof(DatabaseVersionService))]
public void DataServiceGetsDatabaseVersion()
{
var fixture = new Fixture();
var sut = fixture.Build<VersionClientProxy>().FromFactory<VersionClientProxy>((x) =>
{
return new VersionClientProxy(new Uri(ConfigurationManager.AppSettings["DataServiceHostAddress"]));
}).CreateAnonymous();
var version = fixture.Build<DatabaseVersion>().FromFactory<DatabaseVersion>((x) =>
{
return new DatabaseVersion { MajorVersionId = 0, MinorVersionId = 0, BuildVersionId = 0, VersionId = 1 };
}).CreateAnonymous();
var expected = version.AsSource().OfLikeness<DatabaseVersion>();
var actual = (from a in sut.DatabaseVersions
orderby a.VersionId descending
select a).FirstOrDefault();
Assert.True(expected.Equals(actual));
}
这是客户端代理(单独的项目仅引用数据模型项目):
public class VersionClientProxy : DataServiceContext
{
public VersionClientProxy(Uri serviceRoot)
: base(serviceRoot, DataServiceProtocolVersion.V3)
{
}
public IQueryable<DatabaseVersion> DatabaseVersions
{
get { return CreateQuery<DatabaseVersion>("DatabaseVersions"); }
}
}
这是我启动的数据服务(服务在单独的项目中仅引用数据模型项目)
public class UseDataServiceAttribute : BeforeAfterTestAttribute
{
private readonly DataServiceHost serviceHost;
public UseDataServiceAttribute(Type serviceToHost)
{
serviceHost = new DataServiceHost(serviceToHost, new Uri[] { new Uri(ConfigurationManager.AppSettings["DataServiceHostAddress"]) });
}
public override void Before(System.Reflection.MethodInfo methodUnderTest)
{
try { serviceHost.Open(); }
catch
{
try { serviceHost.Close(); }
catch
{
try { serviceHost.Abort(); }
catch { }
}
}
base.Before(methodUnderTest);
}
public override void After(System.Reflection.MethodInfo methodUnderTest)
{
base.After(methodUnderTest);
try { serviceHost.Close(); }
catch
{
try { serviceHost.Abort(); }
catch { }
}
}
}
这是数据模型类(单独的项目,由所有其他项目引用):
public class DatabaseVersionDataModel
{
private readonly List<DatabaseVersion> templist;
public DatabaseVersionDataModel()
{
templist = new List<DatabaseVersion> {
new DatabaseVersion() {
MajorVersionId = 0,
MinorVersionId = 0,
BuildVersionId = 0,
VersionId = 1 }
};
DatabaseVersions = templist.AsQueryable();
}
public IQueryable<DatabaseVersion> DatabaseVersions { get; private set; }
}
这是模型项目中的数据库版本类:
[DataServiceKey("VersionId")]
public class DatabaseVersion
{
public int VersionId { get; set; }
public int MajorVersionId { get; set; }
public int MinorVersionId { get; set; }
public int BuildVersionId { get; set; }
}
这是我的数据服务项目中的 DatabaseVersionService.svc 文件(空 asp.net,从 iisexpress 编译、调试,并且 atom 或 json 是可浏览的):
public class DatabaseVersionService : DataService<DatabaseVersionDataModel>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
// config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
这是删除 throwingrecursionbehavior 并添加 omitonrecursionbehavior 后的输出:
结果消息:System.InvalidCastException:无法将“Ploeh.AutoFixture.Kernel.OmitSpecimen”类型的对象转换为“skeletor.Domain.DatabaseVersion”类型。结果 StackTrace:
在 Ploeh.AutoFixture.Kernel.SpecimenFactory 1.Create2.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.NoSpecimenOutputGuard.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.Postprocessor
(Object request, ISpecimenContext context) 在 Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.<>c_ DisplayClass6.b _1(ISpecimenBuilder b) 在 System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator
2 .MoveNext() 在 System.Linq.Enumerable.d_a5 1 源) 在Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
(Object request, ISpecimenContext context) 在 Ploeh.AutoFixture.Kernel.FilteringSpecimenBuilder.Create(Object request, ISpecimenContext context)在 Ploeh.AutoFixture.Dsl.NodeComposer1.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.<>c__DisplayClass6.<Create>b__1(ISpecimenBuilder b) at System.Linq.Enumerable.WhereSelectListIterator
2.MoveNext() 在 System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.<DefaultIfEmptyIterator>d__a5
1.MoveNext() 在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source) at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Dsl.CompositeNodeComposer
1.Create(Object request, ISpecimenContext context) 在 Ploeh.AutoFixture.Kernel.SpecimenContext。在 Ploeh.AutoFixture.Kernel.SpecimenFactory 处解析(对象请求) 1. 在 Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder 处2.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.NoSpecimenOutputGuard.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.Postprocessor
创建(对象请求,ISpecimenContext 上下文)。<>c _DisplayClass6.b_ 1(ISpecimenBuilder b)在 System.Linq.Enumerable 处。 WhereSelectListIterator2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator
2.MoveNext() 在 System.Linq.Enumerable.d _a51.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 来源)在 Ploeh.AutoFixture.Kernel.FilteringSpecimenBuilder.Create(Object request, ISpecimenContext context) 在 Ploeh.AutoFixture.Dsl.NodeComposer 的 Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)1.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.<>c__DisplayClass6.<Create>b__1(ISpecimenBuilder b) at System.Linq.Enumerable.WhereSelectListIterator
2.MoveNext()在 System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Linq.Enumerable.<DefaultIfEmptyIterator>d__a5
1.MoveNext() 在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source) at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context) at Ploeh.AutoFixture.Dsl.CompositeNodeComposer
1.在 Ploeh.AutoFixture.SpecimenFactory.CreateAnonymous[T](ISpecimenContext context, T 种子) 在 Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request) 在 Ploeh.AutoFixture.SpecimenFactory.CreateAnonymous 处创建(对象请求,ISpecimenContext 上下文) [T](ISpecimenContext context) at Ploeh.AutoFixture.SpecimenFactory.CreateAnonymous[T](ISpecimenBuilderComposer composer) at Ploeh.AutoFixture.SpecimenFactory.CreateAnonymous[T](IPostprocessComposer`1 composer) at skeletor.AcceptanceTests.InfrstructureTests.DataServiceGetsDatabaseVersion() in d:\projects\skeletor\skeletor.AcceptanceTests\InfrstructureTests.cs:第 31 行