add a message parameter to assertions and expectations

This commit is contained in:
Stefan Dragnev 2015-08-11 15:45:27 +03:00
Родитель 509ea39201
Коммит f2f077999f
16 изменённых файлов: 255 добавлений и 147 удалений

Просмотреть файл

@ -805,6 +805,38 @@ namespace Telerik.JustMock.Tests
Mock.NonPublic.Assert(target, "DoB", Occurs.Once());
}
[TestMethod]
public void ShouldIncludeMessageInPosthocAssertion()
{
var x = Mock.Create<IDisposable>();
var ex = Assert.Throws<AssertionException>(() => Mock.Assert(() => x.Dispose(), "The message!"));
Assert.True(ex.Message.Contains("The message!"));
}
[TestMethod]
public void ShouldIncludeMessageInBlanketAssertionWithMultipleFailures()
{
var x = Mock.Create<IDisposable>();
Mock.Arrange(() => x.Dispose()).MustBeCalled("Because of reasons!");
Mock.Arrange(() => x.Dispose()).InOrder("More reasons!");
var ex = Assert.Throws<AssertionException>(() => Mock.Assert(x, "The blanket!"));
Assert.True(ex.Message.Contains("Because of reasons!"));
Assert.True(ex.Message.Contains("More reasons!"));
Assert.True(ex.Message.Contains("The blanket!"));
}
[TestMethod]
public void ShouldIncludeMessageInBlanketAssertionWithSingleFailure()
{
var x = Mock.Create<IDisposable>();
Mock.Arrange(() => x.Dispose()).MustBeCalled("Because of reasons!");
var ex = Assert.Throws<AssertionException>(() => Mock.Assert(x, "The blanket!"));
Assert.True(ex.Message.Contains("Because of reasons!"));
Assert.True(ex.Message.Contains("The blanket!"));
}
public class FooWithSetThatThows
{
public virtual int Value
@ -836,7 +868,6 @@ namespace Telerik.JustMock.Tests
void RequestNavigate(RegionNames names, FooExrepssion exp);
}
public class FooExrepssion
{
public virtual void Update(IEnumerable<string> arguments)

Просмотреть файл

@ -468,5 +468,19 @@ namespace Telerik.JustMock.Tests
var obj = c.Instance;
Assert.NotNull(obj.Dep);
}
[TestMethod, TestCategory("Lite"), TestCategory("AutoMock")]
public void ShouldIncludeAssertionMessageWhenAssertingContainer()
{
var c = new MockingContainer<FileLog>();
c.Arrange<ICalendar>(x => x.Now).MustBeCalled("Calendar must be used!");
c.Arrange<IFileSystem>(x => x.Refresh()).MustBeCalled("Should use latest data!");
var ex = Assert.Throws<AssertFailedException>(() => c.Assert("Container must be alright!"));
Assert.True(ex.Message.Contains("Calendar must be used!"));
Assert.True(ex.Message.Contains("Should use latest data!"));
Assert.True(ex.Message.Contains("Container must be alright!"));
}
}
}

Просмотреть файл

@ -77,7 +77,7 @@ namespace Telerik.JustMock.AutoMock
public void ForEachMock(Action<object> action)
{
using (MockingContext.BeginFailureAggregation())
using (MockingContext.BeginFailureAggregation(null))
{
foreach (var mock in this.mocks)
action(mock);

Просмотреть файл

@ -200,18 +200,20 @@ namespace Telerik.JustMock.AutoMock
/// <summary>
/// Asserts all expected setups.
/// </summary>
public void AssertAll()
/// <param name="message">A message to display if the assertion fails.</param>
public void AssertAll(string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.mockResolver.ForEachMock(mock => mock.AssertAll()));
ProfilerInterceptor.GuardInternal(() => this.mockResolver.ForEachMock(mock => mock.AssertAll(message)));
}
/// <summary>
/// Asserts all expected calls that are marked as must or
/// to be occurred a certain number of times.
/// </summary>
public void Assert()
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert(string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.mockResolver.ForEachMock(mock => mock.Assert()));
ProfilerInterceptor.GuardInternal(() => this.mockResolver.ForEachMock(mock => mock.Assert(message)));
}
/// <summary>
@ -219,9 +221,10 @@ namespace Telerik.JustMock.AutoMock
/// </summary>
/// <typeparam name="TService">Service type.</typeparam>
/// <param name="expression">Target expression.</param>
public void Assert<TService>(Expression<Action<TService>> expression)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(Expression<Action<TService>> expression, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression, message));
}
/// <summary>
@ -229,9 +232,10 @@ namespace Telerik.JustMock.AutoMock
/// </summary>
/// <typeparam name="TService">Service type.</typeparam>
/// <param name="expression">Target expression</param>
public void Assert<TService>(Expression<Func<TService, object>> expression)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(Expression<Func<TService, object>> expression, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression, message));
}
/// <summary>
@ -247,10 +251,11 @@ namespace Telerik.JustMock.AutoMock
/// Asserts a specific dependency
/// </summary>
/// <param name="bindingName">Name.</param>
/// <param name="message">A message to display if the assertion fails.</param>
/// <typeparam name="TService">Service Type.</typeparam>
public void Assert<TService>(string bindingName)
public void Assert<TService>(string bindingName, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert());
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(message));
}
/// <summary>
@ -259,9 +264,10 @@ namespace Telerik.JustMock.AutoMock
/// <typeparam name="TService">Service Type.</typeparam>
/// <param name="expression">Target expression.</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public void Assert<TService>(Expression<Func<TService, object>> expression, Occurs occurs)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(Expression<Func<TService, object>> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression, occurs));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression, occurs, message));
}
/// <summary>
@ -270,9 +276,10 @@ namespace Telerik.JustMock.AutoMock
/// <typeparam name="TService">Service Type.</typeparam>
/// <param name="expression">Target expression</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public void Assert<TService>(Expression<Action<TService>> expression, Occurs occurs)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(Expression<Action<TService>> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression, occurs));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>().Assert(expression, occurs, message));
}
/// <summary>
@ -281,9 +288,10 @@ namespace Telerik.JustMock.AutoMock
/// <typeparam name="TService">Service Type.</typeparam>
/// <param name="bindingName">Name.</param>
/// <param name="expression">Target expression.</param>
public void Assert<TService>(string bindingName, Expression<Func<TService, object>> expression)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(string bindingName, Expression<Func<TService, object>> expression, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression, message));
}
/// <summary>
@ -292,9 +300,10 @@ namespace Telerik.JustMock.AutoMock
/// <typeparam name="TService">Service Type.</typeparam>
/// <param name="bindingName">Name.</param>
/// <param name="expression">Target expression.</param>
public void Assert<TService>(string bindingName, Expression<Action<TService>> expression)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(string bindingName, Expression<Action<TService>> expression, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression, message));
}
/// <summary>
@ -304,9 +313,10 @@ namespace Telerik.JustMock.AutoMock
/// <param name="bindingName">Name.</param>
/// <param name="expression">Target expression.</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public void Assert<TService>(string bindingName, Expression<Func<TService, object>> expression, Occurs occurs)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(string bindingName, Expression<Func<TService, object>> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression, occurs));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression, occurs, message));
}
/// <summary>
@ -316,9 +326,10 @@ namespace Telerik.JustMock.AutoMock
/// <param name="bindingName">Name.</param>
/// <param name="expression">Target expression.</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public void Assert<TService>(string bindingName, Expression<Action<TService>> expression, Occurs occurs)
/// <param name="message">A message to display if the assertion fails.</param>
public void Assert<TService>(string bindingName, Expression<Action<TService>> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression, occurs));
ProfilerInterceptor.GuardInternal(() => this.Get<TService>(bindingName).Assert(expression, occurs, message));
}
}
}

Просмотреть файл

@ -25,18 +25,20 @@ namespace Telerik.JustMock.Core.Behaviors
{
private readonly IMockMixin mock;
private readonly int arrangementId;
private readonly string message;
private bool calledInWrongOrder = false;
private bool wasCalled = false;
public string DebugView
{
get { return String.Format("{0}: in-order execution expectation.", IsExpectationMet ? "Met" : "Unmet"); }
get { return String.Format("{0}: in-order execution expectation. {1}", IsExpectationMet ? "Met" : "Unmet", this.message ?? ""); }
}
public InOrderBehavior(IMockMixin mock)
public InOrderBehavior(IMockMixin mock, string message)
{
this.mock = mock;
this.arrangementId = InOrderArrangementCount++;
this.message = message;
}
private int InOrderArrangementCount
@ -76,14 +78,16 @@ namespace Telerik.JustMock.Core.Behaviors
if (this.calledInWrongOrder)
{
MockingContext.Fail("Last call executed out of order. Order of calls so far:\n{1}", invocation.InputToString(), InOrderExecutionMessage);
MockingContext.Fail("{0}Last call executed out of order. Order of calls so far:\n{1}",
this.message != null ? this.message + " " : "", InOrderExecutionMessage);
}
}
public void Assert()
{
if (!IsExpectationMet)
MockingContext.Fail("Calls should be executed in the order they are expected. Actual order of calls:\n{0}", InOrderExecutionMessage);
MockingContext.Fail("{0}Calls should be executed in the order they are expected. Actual order of calls:\n{1}",
this.message != null ? this.message + " " : "", InOrderExecutionMessage);
}
private bool IsExpectationMet

Просмотреть файл

@ -29,6 +29,7 @@ namespace Telerik.JustMock.Core.Behaviors
public int? LowerBound { get; set; }
public int? UpperBound { get; set; }
private string message;
private int calls;
public string DebugView
@ -38,11 +39,12 @@ namespace Telerik.JustMock.Core.Behaviors
if ((LowerBound == null || LowerBound <= 0) && (UpperBound == null))
return null;
return String.Format("{3}: Occurences must be in [{0}, {1}]; calls so far: {2}.",
return String.Format("{3}: Occurences must be in [{0}, {1}]; calls so far: {2}. {4}",
LowerBound.HasValue ? (object)LowerBound.Value : "any",
UpperBound.HasValue ? (object)UpperBound.Value : "any",
calls,
IsInRange(LowerBound, UpperBound, calls) ? "Met" : "Unmet");
IsInRange(LowerBound, UpperBound, calls) ? "Met" : "Unmet",
this.message ?? "");
}
}
@ -51,10 +53,11 @@ namespace Telerik.JustMock.Core.Behaviors
this.methodMock = methodMock;
}
public void SetBounds(int? lowerBound, int? upperBound)
public void SetBounds(int? lowerBound, int? upperBound, string message)
{
this.LowerBound = lowerBound;
this.UpperBound = upperBound;
this.message = message;
}
public void Process(Invocation invocation)
@ -62,7 +65,7 @@ namespace Telerik.JustMock.Core.Behaviors
++calls;
Telerik.JustMock.DebugView.TraceEvent(IndentLevel.DispatchResult, () => String.Format("Calls so far: {0}", calls));
Assert(null, this.UpperBound, calls, null);
Assert(null, this.UpperBound, calls, this.message, null);
}
public void Assert()
@ -73,17 +76,18 @@ namespace Telerik.JustMock.Core.Behaviors
public void Assert(int? lowerBound, int? upperBound)
{
var expr = this.methodMock.ArrangementExpression;
Assert(lowerBound, upperBound, this.calls, expr);
Assert(lowerBound, upperBound, this.calls, this.message, expr);
}
public static void Assert(int? lowerBound, int? upperBound, int calls, object expression)
public static void Assert(int? lowerBound, int? upperBound, int calls, string userMessage, object expression)
{
if (IsInRange(lowerBound, upperBound, calls))
return;
var message = String.Format("Occurrence expectation failed. {0}. Calls so far: {1}",
var message = String.Format("{2}Occurrence expectation failed. {0}. Calls so far: {1}",
MakeRangeString(lowerBound, upperBound),
calls);
calls,
userMessage != null ? userMessage + " " : string.Empty);
if (expression != null)
message += String.Format("\nArrange expression: {0}", expression).EscapeFormatString();

Просмотреть файл

@ -98,15 +98,15 @@ namespace Telerik.JustMock.Core.Context
.Select(p => indent + p.Trim()));
}
public static IDisposable BeginFailureAggregation()
public static IDisposable BeginFailureAggregation(string message)
{
if (failureAggregator == null)
{
failureAggregator = new FailureAggregator();
failureAggregator = new FailureAggregator(message);
}
else
{
failureAggregator.AddRef();
failureAggregator.AddRef(message);
}
return failureAggregator;
@ -166,10 +166,17 @@ namespace Telerik.JustMock.Core.Context
{
private List<string> failures;
private int references = 1;
private string userMessage;
public void AddRef()
public FailureAggregator(string message)
{
userMessage = message;
}
public void AddRef(string message)
{
references++;
userMessage = message;
}
public void AddFailure(string msg)
@ -194,9 +201,11 @@ namespace Telerik.JustMock.Core.Context
return;
if (failures.Count == 1)
Fail(failures[0]);
Fail((userMessage != null ? userMessage + Environment.NewLine : null) + failures[0]);
var sb = new StringBuilder();
if (userMessage != null)
sb.AppendLine(userMessage);
sb.AppendLine("Multiple assertion failures:");
for (int i = 0; i < failures.Count; ++i)
{

Просмотреть файл

@ -729,18 +729,18 @@ namespace Telerik.JustMock.Core
}
}
internal void AssertAll(object mock)
internal void AssertAll(string message, object mock)
{
using (MockingContext.BeginFailureAggregation())
using (MockingContext.BeginFailureAggregation(message))
{
var mocks = GetMethodMocksFromObject(mock);
AssertBehaviorsForMocks(mocks.Select(m => m.MethodMock), false);
}
}
internal void Assert(object mock, Expression expr = null, Args args = null, Occurs occurs = null)
internal void Assert(string message, object mock, Expression expr = null, Args args = null, Occurs occurs = null)
{
using (MockingContext.BeginFailureAggregation())
using (MockingContext.BeginFailureAggregation(message))
{
if (expr == null)
{
@ -770,27 +770,27 @@ namespace Telerik.JustMock.Core
}
}
internal void AssertAction(Action memberAction, Args args = null, Occurs occurs = null)
internal void AssertAction(string message, Action memberAction, Args args = null, Occurs occurs = null)
{
using (MockingContext.BeginFailureAggregation())
using (MockingContext.BeginFailureAggregation(message))
{
var callPattern = ConvertActionToCallPattern(memberAction);
AssertForCallPattern(callPattern, args, occurs);
}
}
internal void AssertMethodInfo(object instance, MethodBase method, object[] arguments, Occurs occurs)
internal void AssertMethodInfo(string message, object instance, MethodBase method, object[] arguments, Occurs occurs)
{
using (MockingContext.BeginFailureAggregation())
using (MockingContext.BeginFailureAggregation(message))
{
var callPattern = ConvertMethodInfoToCallPattern(instance, method, arguments);
AssertForCallPattern(callPattern, null, occurs);
}
}
internal void AssertIgnoreInstance(Type type, bool ignoreMethodMockOccurrences)
internal void AssertIgnoreInstance(string message, Type type, bool ignoreMethodMockOccurrences)
{
using (MockingContext.BeginFailureAggregation())
using (MockingContext.BeginFailureAggregation(message))
{
var methodMocks = GetMethodMocksFromObject(null, type);
AssertBehaviorsForMocks(methodMocks.Select(m => m.MethodMock), ignoreMethodMockOccurrences);
@ -881,7 +881,7 @@ namespace Telerik.JustMock.Core
var mocks = CountMethodMockInvocations(callPattern, args, out callsCount);
if (occurs != null)
{
InvocationOccurrenceBehavior.Assert(occurs.LowerBound, occurs.UpperBound, callsCount, null);
InvocationOccurrenceBehavior.Assert(occurs.LowerBound, occurs.UpperBound, callsCount, null, null);
}
if (mocks.Count == 0)
@ -898,7 +898,7 @@ namespace Telerik.JustMock.Core
}
if (occurs == null && mocks.Count == 0)
{
InvocationOccurrenceBehavior.Assert(Occurs.AtLeastOnce().LowerBound, Occurs.AtLeastOnce().UpperBound, callsCount, null);
InvocationOccurrenceBehavior.Assert(Occurs.AtLeastOnce().LowerBound, Occurs.AtLeastOnce().UpperBound, callsCount, null, null);
}
else
{

Просмотреть файл

@ -25,6 +25,6 @@ namespace Telerik.JustMock.Expectations.Abstraction
/// <summary>
/// Specifies that the mock call should be invoked to pass <see cref="Mock.Assert{T}(T)"/>
/// </summary>
void MustBeCalled();
void MustBeCalled(string message = null);
}
}

Просмотреть файл

@ -398,7 +398,8 @@ namespace Telerik.JustMock.Expectations.Abstraction
/// </summary>
/// <param name="dynamicExpression">An expression built using a wrapper returned by Wrap.</param>
/// <param name="occurs">Occurrence expectation to assert.</param>
void Assert(dynamic dynamicExpression, Occurs occurs);
/// <param name="message">A message to display if the assertion fails.</param>
void Assert(dynamic dynamicExpression, Occurs occurs, string message = null);
/// <summary>
/// Asserts an expectation given using a dynamic wrapper built with Wrap()
@ -406,6 +407,7 @@ namespace Telerik.JustMock.Expectations.Abstraction
/// <param name="dynamicExpression">An expression built using a wrapper returned by Wrap.</param>
/// <param name="args">Additional arguments to clarify the assertion expression.</param>
/// <param name="occurs">Occurrence expectation to assert.</param>
void Assert(dynamic dynamicExpression, Args args, Occurs occurs);
/// <param name="message">A message to display if the assertion fails.</param>
void Assert(dynamic dynamicExpression, Args args, Occurs occurs, string message = null);
}
}

Просмотреть файл

@ -31,28 +31,28 @@ namespace Telerik.JustMock.Expectations.Abstraction
/// Specifies how many times the call should occur.
/// </summary>
/// <param name="numberOfTimes">Specified number of times</param>
void Occurs(int numberOfTimes);
void Occurs(int numberOfTimes, string message = null);
/// <summary>
/// Specifies how many times at least the call should occur.
/// </summary>
/// <param name="numberOfTimes">Specified number of times</param>
void OccursAtLeast(int numberOfTimes);
void OccursAtLeast(int numberOfTimes, string message = null);
/// <summary>
/// Specifies how many times maximum the call can occur.
/// </summary>
/// <param name="numberOfTimes">Specified number of times</param>
void OccursAtMost(int numberOfTimes);
void OccursAtMost(int numberOfTimes, string message = null);
/// <summary>
/// Specifies that the call must occur once.
/// </summary>
void OccursOnce();
void OccursOnce(string message = null);
/// <summary>
/// Specifies that the call must never occur.
/// </summary>
void OccursNever();
void OccursNever(string message = null);
}
}

Просмотреть файл

@ -30,6 +30,6 @@ namespace Telerik.JustMock.Expectations.Abstraction
/// <summary>
/// Specifies a call should occur in a specific order.
/// </summary>
IOccurrence InOrder();
IOccurrence InOrder(string message = null);
}
}

Просмотреть файл

@ -323,9 +323,9 @@ namespace Telerik.JustMock.Expectations
/// <summary>
/// Specifies that the arranged member must be called. Asserting the mock will throw if the expectation is not fulfilled.
/// </summary>
public void MustBeCalled()
public void MustBeCalled(string message = null)
{
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(1, null));
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(1, null, message));
}
#endregion
@ -336,43 +336,43 @@ namespace Telerik.JustMock.Expectations
/// Specifies how many times the call should occur.
/// </summary>
/// <param name="numberOfTimes">Specified number of times</param>
public void Occurs(int numberOfTimes)
public void Occurs(int numberOfTimes, string message = null)
{
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(numberOfTimes, numberOfTimes));
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(numberOfTimes, numberOfTimes, message));
}
/// <summary>
/// Specifies how many times atleast the call should occur.
/// </summary>
/// <param name="numberOfTimes">Specified number of times</param>
public void OccursAtLeast(int numberOfTimes)
public void OccursAtLeast(int numberOfTimes, string message = null)
{
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(numberOfTimes, null));
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(numberOfTimes, null, message));
}
/// <summary>
/// Specifies how many times maximum the call can occur.
/// </summary>
/// <param name="numberOfTimes">Specified number of times</param>
public void OccursAtMost(int numberOfTimes)
public void OccursAtMost(int numberOfTimes, string message = null)
{
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(null, numberOfTimes));
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(null, numberOfTimes, message));
}
/// <summary>
/// Specifies that the call must occur once.
/// </summary>
public void OccursOnce()
public void OccursOnce(string message = null)
{
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(1, 1));
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(1, 1, message));
}
/// <summary>
/// Specifies that the call must never occur.
/// </summary>
public void OccursNever()
public void OccursNever(string message = null)
{
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(0, 0));
ProfilerInterceptor.GuardInternal(() => occurences.SetBounds(0, 0, message));
}
#endregion
@ -396,11 +396,11 @@ namespace Telerik.JustMock.Expectations
/// <summary>
/// Specifies a call should occur in a specific order.
/// </summary>
public IOccurrence InOrder()
public IOccurrence InOrder(string message = null)
{
return ProfilerInterceptor.GuardInternal(() =>
{
this.behaviors.Add(new InOrderBehavior(this.Mock));
this.behaviors.Add(new InOrderBehavior(this.Mock, message));
return this;
});
}

Просмотреть файл

@ -213,6 +213,11 @@ namespace Telerik.JustMock.Expectations
return candidates.FirstOrDefault(method => method.ArgumentsMatchSignature(args) && ReturnTypeMatches(returnType, method));
}
private static string GetAssertionMessage(object[] args)
{
return null;
}
public ActionExpectation Arrange(object target, string memberName, params object[] args)
{
return ProfilerInterceptor.GuardInternal(() =>
@ -260,14 +265,19 @@ namespace Telerik.JustMock.Expectations
if (mixin != null)
type = mixin.DeclaringType;
var message = GetAssertionMessage(args);
var method = GetMethodByName(type, typeof(TReturn), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(target, method, args, null);
MockingContext.CurrentRepository.AssertMethodInfo(message, target, method, args, null);
});
}
public void Assert(object target, MethodInfo method, params object[] args)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertMethodInfo(target, method, args, null));
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
MockingContext.CurrentRepository.AssertMethodInfo(message, target, method, args, null);
});
}
public void Assert(object target, string memberName, params object[] args)
@ -279,8 +289,9 @@ namespace Telerik.JustMock.Expectations
if (mixin != null)
type = mixin.DeclaringType;
var message = GetAssertionMessage(args);
var method = GetMethodByName(type, typeof(void), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(target, method, args, null);
MockingContext.CurrentRepository.AssertMethodInfo(message, target, method, args, null);
});
}
@ -293,14 +304,19 @@ namespace Telerik.JustMock.Expectations
if (mixin != null)
type = mixin.DeclaringType;
var message = GetAssertionMessage(args);
var method = GetMethodByName(type, typeof(TReturn), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(target, method, args, occurs);
MockingContext.CurrentRepository.AssertMethodInfo(message, target, method, args, occurs);
});
}
public void Assert(object target, MethodInfo method, Occurs occurs, params object[] args)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertMethodInfo(target, method, args, occurs));
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
MockingContext.CurrentRepository.AssertMethodInfo(message, target, method, args, occurs);
});
}
public void Assert(object target, string memberName, Occurs occurs, params object[] args)
@ -312,8 +328,9 @@ namespace Telerik.JustMock.Expectations
if (mixin != null)
type = mixin.DeclaringType;
var message = GetAssertionMessage(args);
var method = GetMethodByName(type, typeof(void), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(target, method, args, occurs);
MockingContext.CurrentRepository.AssertMethodInfo(message, target, method, args, occurs);
});
}
@ -389,22 +406,28 @@ namespace Telerik.JustMock.Expectations
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(typeof(T), typeof(void), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, occurs);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, occurs);
});
}
public void Assert(MethodBase method, Occurs occurs, params object[] args)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, occurs));
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, occurs);
});
}
public void Assert<T, TReturn>(string memberName, Occurs occurs, params object[] args)
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(typeof(T), typeof(TReturn), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, occurs);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, occurs);
});
}
@ -412,22 +435,28 @@ namespace Telerik.JustMock.Expectations
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(typeof(T), typeof(void), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, null);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, null);
});
}
public void Assert(MethodBase method, params object[] args)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, null));
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, null);
});
}
public void Assert<T, TReturn>(string memberName, params object[] args)
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(typeof(T), typeof(TReturn), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, null);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, null);
});
}
@ -435,8 +464,9 @@ namespace Telerik.JustMock.Expectations
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(targetType, typeof(void), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, occurs);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, occurs);
});
}
@ -444,8 +474,9 @@ namespace Telerik.JustMock.Expectations
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(targetType, typeof(TReturn), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, occurs);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, occurs);
});
}
@ -453,8 +484,9 @@ namespace Telerik.JustMock.Expectations
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(targetType, typeof(void), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, null);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, null);
});
}
@ -462,8 +494,9 @@ namespace Telerik.JustMock.Expectations
{
ProfilerInterceptor.GuardInternal(() =>
{
var message = GetAssertionMessage(args);
var method = GetMethodByName(targetType, typeof(TReturn), memberName, ref args);
MockingContext.CurrentRepository.AssertMethodInfo(null, method, args, null);
MockingContext.CurrentRepository.AssertMethodInfo(message, null, method, args, null);
});
}
@ -563,17 +596,17 @@ namespace Telerik.JustMock.Expectations
);
}
public void Assert(dynamic dynamicExpression, Occurs occurs)
public void Assert(dynamic dynamicExpression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
MockingContext.CurrentRepository.Assert(null, ((IExpressionContainer)dynamicExpression).ToLambda(), null, occurs)
MockingContext.CurrentRepository.Assert(message, null, ((IExpressionContainer)dynamicExpression).ToLambda(), null, occurs)
);
}
public void Assert(dynamic dynamicExpression, Args args, Occurs occurs)
public void Assert(dynamic dynamicExpression, Args args, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
MockingContext.CurrentRepository.Assert(null, ((IExpressionContainer)dynamicExpression).ToLambda(), args, occurs)
MockingContext.CurrentRepository.Assert(message, null, ((IExpressionContainer)dynamicExpression).ToLambda(), args, occurs)
);
}
}

Просмотреть файл

@ -41,7 +41,7 @@ namespace Telerik.JustMock.Helpers
return repo.Arrange(parameterlessArrangeStmt, containerFactory);
}
private static void DoAssert(object obj, Type objType, LambdaExpression expression, Args args, Occurs occurs)
private static void DoAssert(string message, object obj, Type objType, LambdaExpression expression, Args args, Occurs occurs)
{
var repo = MockingContext.CurrentRepository;
Expression parameterlessArrangeStmt = null;
@ -52,7 +52,7 @@ namespace Telerik.JustMock.Helpers
var parameterlessBody = ExpressionReplacer.Replace(expression.Body, instanceParam, instanceConstant);
parameterlessArrangeStmt = Expression.Lambda(parameterlessBody);
}
repo.Assert(obj, parameterlessArrangeStmt, args, occurs);
repo.Assert(message, obj, parameterlessArrangeStmt, args, occurs);
}
/// <summary>
@ -127,11 +127,11 @@ namespace Telerik.JustMock.Helpers
/// <typeparam name="TReturn">Return type for the target setup.</typeparam>
/// <param name="obj">Target object.</param>
/// <param name="action">Target action.</param>
public static void Assert<T, TReturn>(this T obj, Expression<Func<T, TReturn>> action)
public static void Assert<T, TReturn>(this T obj, Expression<Func<T, TReturn>> action, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
obj.Assert(action, null);
obj.Assert(action, null, message);
});
}
@ -143,11 +143,11 @@ namespace Telerik.JustMock.Helpers
/// <param name="obj">Target object.</param>
/// <param name="expression">Target expression</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public static void Assert<T, TReturn>(this T obj, Expression<Func<T, TReturn>> expression, Occurs occurs)
public static void Assert<T, TReturn>(this T obj, Expression<Func<T, TReturn>> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
DoAssert(obj, typeof(T), expression, null, occurs);
DoAssert(message, obj, typeof(T), expression, null, occurs);
});
}
@ -157,11 +157,11 @@ namespace Telerik.JustMock.Helpers
/// <typeparam name="T">Type of the mock.</typeparam>
/// <param name="obj">Target object.</param>
/// <param name="action">Target action.</param>
public static void Assert<T>(this T obj, Expression<Action<T>> action)
public static void Assert<T>(this T obj, Expression<Action<T>> action, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
DoAssert(obj, typeof(T), action, null, null);
DoAssert(message, obj, typeof(T), action, null, null);
});
}
@ -172,11 +172,11 @@ namespace Telerik.JustMock.Helpers
/// <param name="obj">Target mock object</param>
/// <param name="expression">Target expression</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public static void Assert<T>(this T obj, Expression<Action<T>> expression, Occurs occurs)
public static void Assert<T>(this T obj, Expression<Action<T>> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
DoAssert(obj, typeof(T), expression, null, occurs);
DoAssert(message, obj, typeof(T), expression, null, occurs);
});
}
@ -228,9 +228,9 @@ namespace Telerik.JustMock.Helpers
/// </summary>
/// <typeparam name="T">Type of the mock.</typeparam>
/// <param name="target">Target instance.</param>
public static void Assert<T>(this T target)
public static void Assert<T>(this T target, string message = null)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.Assert(target));
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.Assert(message, target));
}
/// <summary>
@ -238,9 +238,9 @@ namespace Telerik.JustMock.Helpers
/// </summary>
/// <typeparam name="T">Type of the mock.</typeparam>
/// <param name="target">Target instance.</param>
public static void AssertAll<T>(this T target)
public static void AssertAll<T>(this T target, string message = null)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertAll(target));
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertAll(message, target));
}
}
}

Просмотреть файл

@ -29,11 +29,11 @@ namespace Telerik.JustMock
/// </summary>
/// <param name="expression">Target expression</param>
/// <typeparam name="TReturn">Return type for the assert expression</typeparam>
public static void Assert<TReturn>(Expression<Func<TReturn>> expression)
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression);
MockingContext.CurrentRepository.Assert(message, null, expression);
});
}
@ -43,11 +43,11 @@ namespace Telerik.JustMock
/// <param name="expression">Target expression</param>
/// <typeparam name="TReturn">Return type for the assert expression</typeparam>
/// <param name="args">Assert argument</param>
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args)
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression, args, null);
MockingContext.CurrentRepository.Assert(message, null, expression, args, null);
});
}
@ -58,11 +58,11 @@ namespace Telerik.JustMock
/// <param name="expression">Target expression</param>
/// <param name="occurs">Specifies how many times a call has occurred</param>
/// <typeparam name="TReturn">Return type for the target call</typeparam>
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Occurs occurs)
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression, null, occurs);
MockingContext.CurrentRepository.Assert(message, null, expression, null, occurs);
});
}
@ -73,11 +73,11 @@ namespace Telerik.JustMock
/// <param name="args">Specifies to ignore the instance and/or arguments during assertion.</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
/// <typeparam name="TReturn">Return type for the target call</typeparam>
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, Occurs occurs)
public static void Assert<TReturn>(Expression<Func<TReturn>> expression, Args args, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression, args, occurs);
MockingContext.CurrentRepository.Assert(message, null, expression, args, occurs);
});
}
@ -85,11 +85,11 @@ namespace Telerik.JustMock
/// Asserts a specific call from expression.
/// </summary>
/// <param name="expression">Action expression defining the action to verify.</param>
public static void Assert(Expression<Action> expression)
public static void Assert(Expression<Action> expression, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression);
MockingContext.CurrentRepository.Assert(message, null, expression);
});
}
@ -98,11 +98,11 @@ namespace Telerik.JustMock
/// </summary>
/// <param name="expression">The action to verify.</param>
/// <param name="args">Specifies to ignore the instance and/or arguments during assertion.</param>
public static void Assert(Expression<Action> expression, Args args)
public static void Assert(Expression<Action> expression, Args args, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression, args, null);
MockingContext.CurrentRepository.Assert(message, null, expression, args, null);
});
}
@ -111,11 +111,11 @@ namespace Telerik.JustMock
/// </summary>
/// <param name="expression">The action to verify.</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public static void Assert(Expression<Action> expression, Occurs occurs)
public static void Assert(Expression<Action> expression, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression, null, occurs);
MockingContext.CurrentRepository.Assert(message, null, expression, null, occurs);
});
}
@ -125,11 +125,11 @@ namespace Telerik.JustMock
/// <param name="expression">The action to verify.</param>
/// <param name="args">Specifies to ignore the instance and/or arguments during assertion.</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public static void Assert(Expression<Action> expression, Args args, Occurs occurs)
public static void Assert(Expression<Action> expression, Args args, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.Assert(null, expression, args, occurs);
MockingContext.CurrentRepository.Assert(message, null, expression, args, occurs);
});
}
@ -140,11 +140,11 @@ namespace Telerik.JustMock
/// <param name="func">Contains the target mock call</param>
/// <typeparam name="T">Target type</typeparam>
/// <typeparam name="TResult">The type of the return value of the method</typeparam>
public static void Assert<T, TResult>(T target, Func<T, TResult> func)
public static void Assert<T, TResult>(T target, Func<T, TResult> func, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.AssertAction(() => func(target));
MockingContext.CurrentRepository.AssertAction(message, () => func(target));
});
}
@ -156,11 +156,11 @@ namespace Telerik.JustMock
/// <param name="func">Contains the target mock call</param>
/// <typeparam name="T">Target type</typeparam>
/// <typeparam name="TResult">The type of the return value of the method</typeparam>
public static void Assert<T, TResult>(T target, Func<T, TResult> func, Occurs occurs)
public static void Assert<T, TResult>(T target, Func<T, TResult> func, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.AssertAction(() => func(target), null, occurs);
MockingContext.CurrentRepository.AssertAction(message, () => func(target), null, occurs);
});
}
@ -168,11 +168,11 @@ namespace Telerik.JustMock
/// Asserts the specific property set operation.
/// </summary>
/// <param name="action">Action defining the set operation</param>
public static void AssertSet(Action action)
public static void AssertSet(Action action, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.AssertAction(action);
MockingContext.CurrentRepository.AssertAction(message, action);
});
}
@ -181,11 +181,11 @@ namespace Telerik.JustMock
/// </summary>
/// <param name="action">Action defining the set operation</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public static void AssertSet(Action action, Occurs occurs)
public static void AssertSet(Action action, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.AssertAction(action, null, occurs);
MockingContext.CurrentRepository.AssertAction(message, action, null, occurs);
});
}
@ -194,11 +194,11 @@ namespace Telerik.JustMock
/// </summary>
/// <param name="action">Action defining the set operation</param>
/// <param name="args">Specifies to ignore the instance and/or arguments during assertion.</param>
public static void AssertSet(Action action, Args args)
public static void AssertSet(Action action, Args args, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.AssertAction(action, args, null);
MockingContext.CurrentRepository.AssertAction(message, action, args, null);
});
}
@ -208,11 +208,11 @@ namespace Telerik.JustMock
/// <param name="action">Action defining the set operation</param>
/// <param name="args">Specifies to ignore the instance and/or arguments during assertion.</param>
/// <param name="occurs">Specifies the number of times a mock call should occur.</param>
public static void AssertSet(Action action, Args args, Occurs occurs)
public static void AssertSet(Action action, Args args, Occurs occurs, string message = null)
{
ProfilerInterceptor.GuardInternal(() =>
{
MockingContext.CurrentRepository.AssertAction(action, args, occurs);
MockingContext.CurrentRepository.AssertAction(message, action, args, occurs);
});
}
@ -222,9 +222,9 @@ namespace Telerik.JustMock
/// </summary>
/// <typeparam name="T">Target type</typeparam>
/// <param name="mocked">Target instance</param>
public static void Assert<T>(T mocked)
public static void Assert<T>(T mocked, string message = null)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.Assert(mocked));
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.Assert(message, mocked));
}
/// <summary>
@ -232,27 +232,27 @@ namespace Telerik.JustMock
/// </summary>
/// <typeparam name="T">Target type</typeparam>
/// <param name="mocked">Target instance</param>
public static void AssertAll<T>(T mocked)
public static void AssertAll<T>(T mocked, string message = null)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertAll(mocked));
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertAll(message, mocked));
}
/// <summary>
/// Asserts all expectation on the given type
/// </summary>
/// <param name="type">The type which declared the methods to assert.</param>
public static void Assert(Type type)
public static void Assert(Type type, string message = null)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertIgnoreInstance(type, ignoreMethodMockOccurrences: false));
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertIgnoreInstance(message, type, ignoreMethodMockOccurrences: false));
}
/// <summary>
/// Asserts all expectation on the given type
/// </summary>
/// <typeparam name="T">The type which declared the methods to assert.</typeparam>
public static void Assert<T>()
public static void Assert<T>(string message = null)
{
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertIgnoreInstance(typeof(T), ignoreMethodMockOccurrences: false));
ProfilerInterceptor.GuardInternal(() => MockingContext.CurrentRepository.AssertIgnoreInstance(message, typeof(T), ignoreMethodMockOccurrences: false));
}
/// <summary>