File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
AutofacContrib.NSubstitute.Tests
AutofacContrib.NSubstitute/MockHandlers Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 1
1
using Autofac ;
2
2
using Autofac . Core ;
3
+ using Autofac . Core . Registration ;
3
4
using NUnit . Framework ;
4
5
using System ;
5
6
@@ -74,6 +75,23 @@ public void InjectPropertiesClass()
74
75
Assert . IsNotNull ( obj . Service ) ;
75
76
}
76
77
78
+ [ Test ]
79
+ public void InjectPropertiesNotResolvable ( )
80
+ {
81
+ using var mock = AutoSubstitute . Configure ( )
82
+ . InjectProperties ( )
83
+ . SubstituteFor < WithStringProperty > ( )
84
+ . Build ( )
85
+ . Container ;
86
+
87
+ // Verify that strings are not resolvable, otherwise this test won't be valid.
88
+ Assert . Throws < ComponentNotRegisteredException > ( ( ) => mock . Resolve < string > ( ) ) ;
89
+
90
+ var obj = mock . Resolve < WithStringProperty > ( ) ;
91
+
92
+ Assert . IsNotNull ( obj . Test ) ;
93
+ }
94
+
77
95
[ Test ]
78
96
public void InjectPropertiesInterfaceRecursive ( )
79
97
{
@@ -146,6 +164,11 @@ public class WithProperties
146
164
public ITestInterface1 Service { get ; set ; }
147
165
}
148
166
167
+ public class WithStringProperty
168
+ {
169
+ public virtual string Test { get ; }
170
+ }
171
+
149
172
public class Class1
150
173
{
151
174
}
Original file line number Diff line number Diff line change @@ -37,7 +37,14 @@ public RouteAction Handle(ICall call)
37
37
return RouteAction . Continue ( ) ;
38
38
}
39
39
40
- return RouteAction . Return ( _context . Resolve ( call . GetReturnType ( ) ) ) ;
40
+ var service = _context . ResolveOptional ( call . GetReturnType ( ) ) ;
41
+
42
+ if ( service is null )
43
+ {
44
+ return RouteAction . Continue ( ) ;
45
+ }
46
+
47
+ return RouteAction . Return ( service ) ;
41
48
}
42
49
}
43
50
}
You can’t perform that action at this time.
0 commit comments