我试图发现如何将async和await关键字应用于我的xUnit测试.我使用的是xUnit 1.9和Async CTP 1.3.这是我的测试用例
我有一个接口,它指定一个异步方法调用
public interface IDoStuffAsync
{
Task AnAsyncMethod(string value);
}
Run Code Online (Sandbox Code Playgroud)
我有一个使用接口并调用异步方法的类
public class UseAnAsyncThing
{
private readonly IDoStuffAsync _doStuffAsync;
public UseAnAsyncThing(IDoStuffAsync doStuffAsync)
{
_doStuffAsync = doStuffAsync;
}
public async Task DoThatAsyncOperation(string theValue)
{
await _doStuffAsync.AnAsyncMethod(theValue);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我希望检查方法DoThatAsyncOperation是否使用正确的值调用方法,因此我模拟了接口并使用Moq来验证调用
[Fact]
public async void The_test_will_pass_even_though_it_should_fail()
{
var mock = new Mock<IDoStuffAsync>();
var sut = new UseAnAsyncThing(mock.Object);
mock.Setup(x => x.AnAsyncMethod(It.IsAny<string>()));
await sut.DoThatAsyncOperation("test");
// This won't throw a Moq.MockExcpetion so the test appears to pass
// However it does …Run Code Online (Sandbox Code Playgroud) 是否可以将Unity配置为检测循环引用或拦截类型解析器以显示某些调试信息?
这里有几个相互依赖的接口和类
public interface IThing1 { }
public class Thing1 : IThing1
{
private IThing2 _thing2;
public Thing1(IThing2 thing2)
{
_thing2 = thing2;
}
}
public interface IThing2 { }
public class Thing2 : IThing2
{
private IThing1 _thing1;
public Thing2(IThing1 thing1)
{
_thing1 = thing1;
}
}
Run Code Online (Sandbox Code Playgroud)
如果在Castle Windsor中配置了这两种类型,它将抛出异常并提供一些调试信息以查找循环引用:
Castle.MicroKernel.CircularDependencyException: Dependency cycle has been detected when trying to resolve component 'CircularIoC.Thing1'.
The resolution tree that resulted in the cycle is the following:
Component 'CircularIoC.Thing1' resolved as dependency …Run Code Online (Sandbox Code Playgroud) castle-windsor circular-dependency inversion-of-control unity-container
有没有人知道或者知道将为给定表生成测试数据的SQL脚本?
理想情况下,它将查看表的模式,并根据每列的数据类型创建包含测试数据的行.
如果这不存在,其他人会发现它有用吗?如果是这样的话,我会把手指伸出来写一个.
我正在使用的域模型有很多循环引用.实际上,可以从图中的任何点获取大多数对象.许多这些循环引用也在集合中.因此,一个Booking集合的集合中Students有一个集合,Courses其中包含Bookings等等.这不是真正的模型,只是一个例子.问题是由大约30个不同类别的组合引起的.
要使用此模型,我正在配置和使用AutoFixture
var fixture = new Fixture().Customize(new MultipleCustomization());
fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
fixture.Behaviors.Add(new OmitOnRecursionBehavior());
var booking = fixture.CreateAnonymous<Booking>();
Run Code Online (Sandbox Code Playgroud)
这会导致AutoFixture运行大约20分钟,直到它最终因OutOfMemoryException而失败.
这个模型是否要求AutoFixture创建一个永无止境的无限图?如果是这样,我有什么办法可以配置它来限制图表的深度吗?
我正在开发的项目是使用Entity Framework 4.3和数据迁移来使架构保持最新.在整个项目过程中,迁移文件夹已经增长,现在有600多个文件.这是巨大的.由于所有迁移元数据,我们现在有一个超过12MB的二进制文件.
我想将所有这些内容折叠到一个迁移中并重新开始.我担心的是:
我正在研究一个相当嵌套的模型,它有一些循环引用.它还使用实体框架,因此所有列表都是ICollection<T>.为了适应这种情况,我正在配置AutoFixture,如下所示:
_fixture = new Fixture().Customize(new MultipleCustomization());
_fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
Run Code Online (Sandbox Code Playgroud)
当我尝试创建一个类型
_fixture.CreateAnonymous<Session>();
Run Code Online (Sandbox Code Playgroud)
AutoFixture有问题,并引发以下错误
System.InvalidCastException:无法将类型为'Ploeh.AutoFixture.Kernel.OmitSpecimen'的对象强制转换为'The.Model.Language'
如果我在Session类型中排除集合Language,则AutoFixture会为图中的其他类型抛出相同的异常.
有没有办法从AutoFixture中提取更多信息,例如导致错误的属性?
为什么AutoFixture试图将我的类型转换为OmitSpecimen以及在此过程中可能发生什么以防止它被强制转换?
我在这里为堆栈跟踪创建了一个要点.
我设法重现了这个问题.鉴于这对对象
public class Session
{
public Language Language { get; set; }
}
public class Language
{
public ICollection<Session> Sessions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
调用_fixture.CreateAnonymous<Session>();将抛出强制转换异常.
我当前的项目是使用ASP.Net MVC构建的内部Web应用程序,我正在添加身份验证.我有一个预先构建的HTTPModule,它创建一个具有相应角色的IPrincipal.如果用户未经过身份验证,我会获得一个角色为"Public"的用户对象
由于这是一个内部应用程序,因此大多数页面都是私有的,只能查看角色"Admin".因为我有一个基本控制器,我可以这样做:
[Authorize(Roles="Admin")]
public abstract class MyControllerBase : Controller
{
...
}
Run Code Online (Sandbox Code Playgroud)
我有一个问题,因为有些动作可以在公共网站上查看,如果我将它们归类为:
[Authorize(Roles="Public")]
public class LoginController : MyController
{
public ActionResult Index()
{
}
}
Run Code Online (Sandbox Code Playgroud)
由于未对用户进行身份验证,因此无法加载页面.它似乎是"公共被忽略的继承类的角色.有没有人知道角色是否可以被继承的类重写?
我也试图避免使用Roles ="Admin"归因于所有控制器
谢谢,基思.
c# ×3
autofixture ×2
asp.net ×1
asp.net-mvc ×1
async-await ×1
moq ×1
sql-server ×1
testing ×1
xunit.net ×1