Skip to content

Commit f3a50db

Browse files
committed
#3 Assert an interface resolves to a concrete implementation
1 parent d6aa45c commit f3a50db

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/FluentAssertions.Ioc.Ninject.Tests/KernelAssertionTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using FluentAssertions.Ioc.Ninject.Sample;
3+
using FluentAssertions.Ioc.Samples.Core;
34
using FluentAssertions.Ioc.Samples.Interfaces;
45
using Ninject;
56
using Ninject.Modules;
@@ -126,6 +127,32 @@ public void Should_fail_when_asserting_interfaces_can_be_resolved_when_implement
126127
act.ShouldThrow<AssertionException>();
127128
}
128129

130+
[Test]
131+
public void Should_pass_when_asserting_an_interface_resolves_to_concrete_implementation()
132+
{
133+
// Arrange
134+
var kernel = GetKernel();
135+
136+
// Act
137+
Action act = () => kernel.Should().Resolve<IFooService>().To<FooService>();
138+
139+
// Assert
140+
act.ShouldNotThrow<Exception>();
141+
}
142+
143+
[Test]
144+
public void Should_fail_when_asserting_an_interface_resolves_to_concrete_implementation()
145+
{
146+
// Arrange
147+
var kernel = GetKernel();
148+
149+
// Act
150+
Action act = () => kernel.Should().Resolve<IFooService>().To<FooService2>();
151+
152+
// Assert
153+
act.ShouldThrow<AssertionException>().And.Message.Should().Be(@"Expected interface ""IFooService"" to resolve to type ""FooService2"", but found ""FooService"" instead");
154+
}
155+
129156
private IKernel GetKernel()
130157
{
131158
var modules = new INinjectModule[]
@@ -135,6 +162,14 @@ private IKernel GetKernel()
135162

136163
return new StandardKernel(modules);
137164
}
165+
166+
public class FooService2 : IFooService
167+
{
168+
public void DoWork()
169+
{
170+
throw new NotImplementedException();
171+
}
172+
}
138173

139174
}
140175
}

src/FluentAssertions.Ioc.Ninject/KernelAssertions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ public void WithAtLeastOneInstance()
103103
.FailWith(BuildFailureMessage(failed));
104104
}
105105

106+
/// <summary>
107+
/// Asserts that the specified interface resolves to a concrete type.
108+
/// </summary>
109+
/// <typeparam name="T">The expected concrete type.</typeparam>
110+
public void To<T>()
111+
{
112+
foreach (var type in Types)
113+
{
114+
var actual = Subject.Get(type).GetType();
115+
var expected = typeof (T);
116+
117+
Execute.Assertion.ForCondition(actual == expected)
118+
.FailWith("Expected interface {0} to resolve to type {1}, but found {2} instead", type.Name, expected.Name, actual.Name);
119+
120+
}
121+
}
122+
106123
private string BuildFailureMessage(List<ActivationError> failed)
107124
{
108125
// Build the failure message

0 commit comments

Comments
 (0)