我正在为HttpHandlers做一个项目和写作单元测试。关于运行测试--本行
BackgroundJob.Schedule(() => PokeRequestedChat(c.Id, 1), TimeSpan.FromSeconds(60));给出一个异常ex=> {"JobStorage.Current属性值尚未初始化。必须在使用Hangfire客户端或服务器API之前设置它。“}否则项目在服务器上运行良好。此异常仅在运行xUnit测试时发生。
我是c#的新手,对绞刑一无所知。
发布于 2020-09-08 14:40:55
您不能编写单元测试BackgroundJob.Schedule方法,因为您需要一个Hangfire主机,而hangfire需要在应用程序中注册,您可以尝试为Hangfire.Or编写集成测试,只测试您的PokeRequestedChat方法,或者在BackgroundJob.Schedule上编写一些包装器并对其进行模拟。
发布于 2020-09-08 14:51:01
通过在Test类中添加这一行解决了这个问题
JobStorage.Current = new SqlServerStorage("connectionString"); 发布于 2021-10-29 12:23:19
您可以通过这一行代码解决错误(它为JobStorage.Current创建一个模拟对象):
JobStorage.Current = Substitute.For<SqlServerStorage>(@"Server=.\sqlexpress;Database=test;Trusted_Connection=True;");我在这个示例中使用了NSubstitute,但是您可以使用Moq或其他库。
https://stackoverflow.com/questions/63795249
复制相似问题