File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- using System ;
1+ using Autofac ;
2+ using Autofac . Extensions . DependencyInjection ;
3+ using Blog . Core . IServices ;
4+ using Blog . Core . Services ;
5+ using System ;
26using System . Collections . Generic ;
7+ using System . IO ;
8+ using System . Linq ;
9+ using System . Reflection ;
310using System . Text ;
11+ using Xunit ;
412
5- namespace Blog . Core . Tests . DependencyInjection
13+ namespace Blog . Core . Tests
614{
7- class DI_Test
15+ public class DI_Test
816 {
17+
18+ [ Fact ]
19+ public void DI_Connet_Test ( )
20+ {
21+ //实例化 AutoFac 容器
22+ var builder = new ContainerBuilder ( ) ;
23+ builder . RegisterType < AdvertisementServices > ( ) . As < IAdvertisementServices > ( ) ;
24+
25+ //指定已扫描程序集中的类型注册为提供所有其实现的接口。
26+ var assemblysServices = Assembly . Load ( "Blog.Core.Services" ) ;
27+ builder . RegisterAssemblyTypes ( assemblysServices ) . AsImplementedInterfaces ( ) ;
28+ var assemblysRepository = Assembly . Load ( "Blog.Core.Repository" ) ;
29+ builder . RegisterAssemblyTypes ( assemblysRepository ) . AsImplementedInterfaces ( ) ;
30+
31+
32+ //将services填充到Autofac容器生成器中
33+ //builder.Populate(services);
34+
35+ //使用已进行的组件登记创建新容器
36+ var ApplicationContainer = builder . Build ( ) ;
37+
38+ Assert . True ( ApplicationContainer . ComponentRegistry . Registrations . Count ( ) > 0 ) ;
39+ }
940 }
1041}
Original file line number Diff line number Diff line change 99using System . Linq ;
1010using Blog . Core . Services ;
1111using AutoMapper ;
12+ using Blog . Core . Repository ;
1213
1314namespace Blog . Core . Tests
1415{
1516 public class BlogArticleService_Should
1617 {
1718
18- Mock < IBlogArticleRepository > mockBlogRep = new Mock < IBlogArticleRepository > ( ) ;
19+ Mock < BlogArticleRepository > mockBlogRep = new Mock < BlogArticleRepository > ( ) ;
1920 Mock < IMapper > mockMap = new Mock < IMapper > ( ) ;
2021 BlogArticleServices _blogArticleServices ;
2122
2223 public BlogArticleService_Should ( )
2324 {
24- mockBlogRep . Setup ( r => r . Query ( ) ) ;
25+ // mockBlogRep.Setup(r => r.Query());
2526 _blogArticleServices = new BlogArticleServices ( mockBlogRep . Object , mockMap . Object ) ;
2627 }
2728
@@ -38,7 +39,7 @@ public async void Get_Blogs_Test()
3839 {
3940 var data = await _blogArticleServices . GetBlogs ( ) ;
4041
41- Assert . NotNull ( data ) ;
42+ Assert . True ( data . Any ( ) ) ;
4243 }
4344
4445 [ Fact ]
You can’t perform that action at this time.
0 commit comments