Skip to content

Commit 661d2ed

Browse files
Merge pull request #111 from telerik/stoilov/feature-mock-container-improvements
Implement ArrangeSet and AssertSet methods for MockingContainer
2 parents 4686fda + 64a316a commit 661d2ed

File tree

3 files changed

+172
-25
lines changed

3 files changed

+172
-25
lines changed

Telerik.JustMock.Tests/NinjectAutoMockFixture.cs

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
JustMock Lite
3-
Copyright © 2010-2015 Progress Software Corporation
3+
Copyright © 2010-2015,2019 Progress Software Corporation
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -255,7 +255,10 @@ public void ShouldSelectConstructorBasedOnSettings()
255255
Assert.Throws<MockException>(() => new MockingContainer<VariousCtors>(new AutoMockSettings { ConstructorArgTypes = new[] { typeof(ICalendar) } }));
256256
}
257257

258-
public interface IService { }
258+
public interface IService
259+
{
260+
int Value { get; set; }
261+
}
259262

260263
public class Module
261264
{
@@ -276,6 +279,84 @@ public void ShouldMakeSingletonExplicitlyRequestedServices()
276279
Assert.Same(s1, s2);
277280
}
278281

282+
[TestMethod, TestCategory("Lite"), TestCategory("Ninject")]
283+
public void ShouldArrangePropertySet()
284+
{
285+
// Arrange
286+
var container = new MockingContainer<Module>();
287+
container.ArrangeSet<IService>(x => x.Value = 99).MustBeCalled();
288+
var service = container.Get<IService>();
289+
290+
// Act
291+
service.Value = 99;
292+
293+
// Assert
294+
container.Assert<IService>();
295+
}
296+
297+
[TestMethod, TestCategory("Lite"), TestCategory("Ninject")]
298+
public void ShouldArrangePropertySetWithMatcher()
299+
{
300+
// Arrange
301+
var container = new MockingContainer<Module>();
302+
container.ArrangeSet<IService>(x => x.Value = Arg.AnyInt).MustBeCalled();
303+
var service = container.Get<IService>();
304+
305+
// Act
306+
service.Value = 99;
307+
308+
// Assert
309+
container.Assert<IService>();
310+
}
311+
312+
[TestMethod, TestCategory("Lite"), TestCategory("Ninject")]
313+
public void ShouldAssertPropertySet()
314+
{
315+
// Arrange
316+
var container = new MockingContainer<Module>();
317+
318+
// Act
319+
container.Get<IService>().Value = 99;
320+
321+
// Assert
322+
container.AssertSet<IService>(x => x.Value = 99);
323+
}
324+
325+
[TestMethod, TestCategory("Lite"), TestCategory("Ninject")]
326+
public void ShouldAssertPropertySetWithMatcher()
327+
{
328+
// Arrange
329+
var container = new MockingContainer<Module>();
330+
331+
// Act
332+
container.Get<IService>().Value = 99;
333+
334+
// Assert
335+
container.AssertSet<IService>(x => x.Value = Arg.AnyInt);
336+
}
337+
338+
[TestMethod, TestCategory("Lite"), TestCategory("Ninject")]
339+
public void ShouldAssertPropertySetNegative()
340+
{
341+
DebugView.IsTraceEnabled = true;
342+
343+
// Arrange
344+
var container = new MockingContainer<Module>();
345+
346+
// Assert
347+
container.AssertSet<IService>(x => x.Value = 99, Occurs.Never());
348+
}
349+
350+
[TestMethod, TestCategory("Lite"), TestCategory("Ninject")]
351+
public void ShouldAssertPropertySetNegativeWithMatcher()
352+
{
353+
// Arrange
354+
var container = new MockingContainer<Module>();
355+
356+
// Assert
357+
container.AssertSet<IService>(x => x.Value = Arg.AnyInt, Occurs.Never());
358+
}
359+
279360
[TestMethod, TestCategory("Lite"), TestCategory("AutoMock")]
280361
public void ShouldAssertRaisesAgainstMethod()
281362
{

Telerik.JustMock/AutoMock/MockingContainer.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
JustMock Lite
3-
Copyright © 2010-2015,2018 Progress Software Corporation
3+
Copyright © 2010-2015,2018-2019 Progress Software Corporation
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -98,10 +98,10 @@ private Type ImplementationType
9898
var targetType = typeof(T);
9999
if (targetType.IsAbstract)
100100
{
101-
MockCreationSettings settings = MockCreationSettings.GetSettings(Behavior.CallOriginal);
102-
ProxyTypeInfo typeInfo = MockingContext.CurrentRepository.CreateClassProxyType(targetType, settings);
101+
MockCreationSettings settings = MockCreationSettings.GetSettings(Behavior.CallOriginal);
102+
ProxyTypeInfo typeInfo = MockingContext.CurrentRepository.CreateClassProxyType(targetType, settings);
103103

104-
this.implementationType = typeInfo.ProxyType;
104+
this.implementationType = typeInfo.ProxyType;
105105
foreach (var mixin in typeInfo.Mixins)
106106
{
107107
this.Bind(mixin.Key).ToConstant(mixin.Value);
@@ -198,6 +198,21 @@ public ActionExpectation Arrange<TInterface>(Expression<Action<TInterface>> expr
198198
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().Arrange(expression));
199199
}
200200

201+
/// <summary>
202+
/// Entry-point for setting expectations.
203+
/// </summary>
204+
/// <typeparam name="TInterface">
205+
/// Mocking interface
206+
/// </typeparam>
207+
/// <param name="action">Target action</param>
208+
/// <returns>
209+
/// Reference to <see cref="ActionExpectation"/> to setup the mock.
210+
/// </returns>
211+
public ActionExpectation ArrangeSet<TInterface>(Action<TInterface> action)
212+
{
213+
return ProfilerInterceptor.GuardInternal(() => this.Get<TInterface>().ArrangeSet(action));
214+
}
215+
201216
/// <summary>
202217
/// Asserts all expected setups.
203218
/// </summary>
@@ -332,5 +347,28 @@ public void Assert<TService>(string bindingName, Expression<Action<TService>> ex
332347
{
333348
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression, occurs, message));
334349
}
350+
351+
/// <summary>
352+
/// Asserts the specific call
353+
/// </summary>
354+
/// <typeparam name="TService">Service type.</typeparam>
355+
/// <param name="action">Target action.</param>
356+
/// <param name="message">A message to display if the assertion fails.</param>
357+
public void AssertSet<TService>(Action<TService> action, string message = null)
358+
{
359+
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().AssertSet(action, message));
360+
}
361+
362+
/// <summary>
363+
/// Asserts the specific call
364+
/// </summary>
365+
/// <typeparam name="TService">Service type.</typeparam>
366+
/// <param name="action">Target action.</param>
367+
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
368+
/// <param name="message">A message to display if the assertion fails.</param>
369+
public void AssertSet<TService>(Action<TService> action, Occurs occurs, string message = null)
370+
{
371+
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().AssertSet(action, occurs, message));
372+
}
335373
}
336374
}

Telerik.JustMock/Helpers/FluentHelper.cs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
JustMock Lite
3-
Copyright © 2010-2015 Progress Software Corporation
3+
Copyright © 2010-2015,2019 Progress Software Corporation
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -73,24 +73,6 @@ public static FuncExpectation<TResult> Arrange<T, TResult>(this T obj, Expressio
7373
});
7474
}
7575

76-
/// <summary>
77-
/// Setups target property set operation to act in a specific way.
78-
/// <example>
79-
/// <code>
80-
/// Mock.ArrangeSet(() =&gt;; foo.MyValue = 10).Throws(new InvalidOperationException());
81-
/// </code>
82-
/// This will throw InvalidOperationException for when foo.MyValue is set with 10.
83-
/// </example>
84-
/// </summary>
85-
/// <typeparam name="T">Mock type.</typeparam>
86-
/// <param name="obj">Target mock object.</param>
87-
/// <param name="action">Target action.</param>
88-
/// <returns>Reference to <see cref="ActionExpectation" /> to setup the mock.</returns>
89-
public static ActionExpectation ArrangeSet<T>(this T obj, Action<T> action)
90-
{
91-
return ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.Arrange(() => action(obj), () => new ActionExpectation()));
92-
}
93-
9476
/// <summary>
9577
/// Setups the target call to act in a specific way.
9678
/// </summary>
@@ -106,6 +88,21 @@ public static ActionExpectation Arrange<T>(this T obj, Expression<Action<T>> exp
10688
});
10789
}
10890

91+
/// <summary>
92+
/// Setups target property set operation to act in a specific way.
93+
/// </summary>
94+
/// <typeparam name="T">Mock type</typeparam>
95+
/// <param name="obj">Target mock object</param>
96+
/// <param name="action">Propery set action</param>
97+
/// <returns>Reference to <see cref="ActionExpectation" /> to setup the mock.</returns>
98+
public static ActionExpectation ArrangeSet<T>(this T obj, Action<T> action)
99+
{
100+
return ProfilerInterceptor.GuardInternal(() =>
101+
{
102+
return MockingContext.CurrentRepository.Arrange(() => action(obj), () => new ActionExpectation());
103+
});
104+
}
105+
109106
/// <summary>
110107
/// Arranges the return values of properties and methods according to the given functional specification.
111108
/// </summary>
@@ -180,6 +177,37 @@ public static void Assert<T>(this T obj, Expression<Action<T>> expression, Occur
180177
});
181178
}
182179

180+
/// <summary>
181+
/// Asserts the specific call
182+
/// </summary>
183+
/// <typeparam name="T">Type of the mock.</typeparam>
184+
/// <param name="obj">Target mock object</param>
185+
/// <param name="action">Propert set action</param>
186+
/// <param name="message">A message to display if the assertion fails.</param>
187+
public static void AssertSet<T>(this T obj, Action<T> action, string message = null)
188+
{
189+
ProfilerInterceptor.GuardInternal(() =>
190+
{
191+
MockingContext.CurrentRepository.AssertSetAction(message, () => action(obj));
192+
});
193+
}
194+
195+
/// <summary>
196+
/// Asserts the specific call
197+
/// </summary>
198+
/// <typeparam name="T">Type of the mock.</typeparam>
199+
/// <param name="obj">Target mock object</param>
200+
/// <param name="action">Propert set action</param>
201+
/// <param name="occurs">Specifies the number of times a set action should occur.</param>
202+
/// <param name="message">A message to display if the assertion fails.</param>
203+
public static void AssertSet<T>(this T obj, Action<T> action, Occurs occurs, string message = null)
204+
{
205+
ProfilerInterceptor.GuardInternal(() =>
206+
{
207+
MockingContext.CurrentRepository.AssertSetAction(message, () => action(obj), null, occurs);
208+
});
209+
}
210+
183211
/// <summary>
184212
/// Raises the specified event. If the event is not mocked and is declared on a C# or VB class
185213
/// and has the default implementation for add/remove, then that event can also be raised using this

0 commit comments

Comments
 (0)