Adding .AndDoes() option to Returns() and cleaning up SubState
- Returns() and co. now return ConfiguredCall objs, so additional call configs can be made like .AndDoes(...) for callbacks. - Removing properties in SubstituteState that are actually stateless dependencies. - Removing CallSpecFactory creation cruft from SubstituteState Yes, a factory factory. No, I'm not proud. - Renamed IResultSetter to IConfigureCall Handles configuring results and actions for calls/call specs. - Splitting call spec retrieval logic out from ConfigureCall to new class. GetCallSpec now holds logic of how to get call spec from last call and pending call spec.
This commit is contained in:
Родитель
d76233c197
Коммит
04b0786f70
|
@ -71,6 +71,7 @@
|
|||
<Compile Include="ArgumentInvocationFromMatchers.cs" />
|
||||
<Compile Include="ArgDoFromMatcher.cs" />
|
||||
<Compile Include="AutoValuesForSubs.cs" />
|
||||
<Compile Include="ReturnsAndDoes.cs" />
|
||||
<Compile Include="ExceptionsWhenCheckingReceivedCalls.cs" />
|
||||
<Compile Include="ExceptionsWhenCheckingSequencesOfCalls.cs" />
|
||||
<Compile Include="FieldReports\Issue77_EqualsBehaviourOnClassStubs.cs" />
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace NSubstitute.Acceptance.Specs
|
||||
{
|
||||
public class ReturnsAndDoes
|
||||
{
|
||||
public interface IFoo { IBar GetBar(int i); }
|
||||
public interface IBar { }
|
||||
|
||||
private IFoo sub;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
sub = Substitute.For<IFoo>();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Set_callback_via_extension_method()
|
||||
{
|
||||
var bar = Substitute.For<IBar>();
|
||||
var wasCalled = false;
|
||||
sub.GetBar(2).Returns(bar).AndDoes(x => wasCalled = true);
|
||||
sub.GetBar(1);
|
||||
|
||||
Assert.That(wasCalled, Is.False);
|
||||
var result = sub.GetBar(2);
|
||||
Assert.That(result, Is.EqualTo(bar));
|
||||
Assert.That(wasCalled, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Add_multiple_callbacks_and_ignore_args()
|
||||
{
|
||||
var bar = Substitute.For<IBar>();
|
||||
var wasCalled = false;
|
||||
var argsUsed = new List<int>();
|
||||
sub.GetBar(2)
|
||||
.ReturnsForAnyArgs(bar)
|
||||
.AndDoes(x => wasCalled = true)
|
||||
.AndDoes(x => argsUsed.Add(x.Arg<int>()));
|
||||
|
||||
var result1 = sub.GetBar(42);
|
||||
var result2 = sub.GetBar(9999);
|
||||
Assert.That(wasCalled, Is.True);
|
||||
Assert.That(argsUsed, Is.EquivalentTo(new[] {42, 9999}));
|
||||
Assert.AreSame(result1, bar);
|
||||
Assert.AreSame(result2, bar);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Interaction_with_other_callback_methods()
|
||||
{
|
||||
var calledViaWhenDo = false;
|
||||
var calledViaArgDo = false;
|
||||
var calledViaAndDoes = false;
|
||||
|
||||
sub.GetBar(2).Returns(Substitute.For<IBar>()).AndDoes(x => calledViaAndDoes = true);
|
||||
sub.When(x => x.GetBar(2)).Do(x => calledViaWhenDo = true);
|
||||
sub.GetBar(Arg.Do<int>(x => calledViaArgDo = true));
|
||||
|
||||
Assert.False(calledViaAndDoes && calledViaArgDo && calledViaWhenDo);
|
||||
|
||||
sub.GetBar(2);
|
||||
|
||||
Assert.That(calledViaAndDoes);
|
||||
Assert.That(calledViaArgDo);
|
||||
Assert.That(calledViaWhenDo);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@ namespace NSubstitute.Specs
|
|||
protected readonly object _returnValueFromRecordReplayRoute = "value from record replay route";
|
||||
protected ISubstitutionContext _context;
|
||||
protected ICall _call;
|
||||
protected IResultSetter _resultSetter;
|
||||
protected IConfigureCall ConfigureCall;
|
||||
protected IRouteFactory _routeFactory;
|
||||
protected IReceivedCalls _receivedCalls;
|
||||
protected ISubstituteState _state;
|
||||
|
@ -27,10 +27,10 @@ namespace NSubstitute.Specs
|
|||
_call = mock<ICall>();
|
||||
_state = mock<ISubstituteState>();
|
||||
_receivedCalls = mock<IReceivedCalls>();
|
||||
_resultSetter = mock<IResultSetter>();
|
||||
ConfigureCall = mock<IConfigureCall>();
|
||||
_routeFactory = mock<IRouteFactory>();
|
||||
_state.stub(x => x.ReceivedCalls).Return(_receivedCalls);
|
||||
_state.stub(x => x.ResultSetter).Return(_resultSetter);
|
||||
_state.stub(x => x.ConfigureCall).Return(ConfigureCall);
|
||||
|
||||
var recordReplayRoute = CreateRouteThatReturns(_returnValueFromRecordReplayRoute);
|
||||
recordReplayRoute.stub(x => x.IsRecordReplayRoute).Return(true);
|
||||
|
@ -148,7 +148,7 @@ namespace NSubstitute.Specs
|
|||
[Test]
|
||||
public void Should_set_result()
|
||||
{
|
||||
_resultSetter.received(x => x.SetResultForLastCall(_returnValue, _argMatching));
|
||||
ConfigureCall.received(x => x.SetResultForLastCall(_returnValue, _argMatching));
|
||||
}
|
||||
|
||||
public override void Because()
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
using System;
|
||||
using NSubstitute.Core;
|
||||
using NSubstitute.Specs.Infrastructure;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace NSubstitute.Specs
|
||||
{
|
||||
public class ConfigureCallSpecs
|
||||
{
|
||||
public abstract class Concern : ConcernFor<ConfigureCall>
|
||||
{
|
||||
protected ICallActions _callActions;
|
||||
protected ICallResults _configuredResults;
|
||||
protected IGetCallSpec _getCallSpec;
|
||||
protected IReturn _returnValue;
|
||||
|
||||
public override void Context()
|
||||
{
|
||||
_configuredResults = mock<ICallResults>();
|
||||
_callActions = mock<ICallActions>();
|
||||
_getCallSpec = mock<IGetCallSpec>();
|
||||
|
||||
_returnValue = mock<IReturn>();
|
||||
}
|
||||
|
||||
public override ConfigureCall CreateSubjectUnderTest()
|
||||
{
|
||||
return new ConfigureCall(_configuredResults, _callActions, _getCallSpec);
|
||||
}
|
||||
}
|
||||
|
||||
public class When_setting_result_for_last_call : Concern
|
||||
{
|
||||
[Test]
|
||||
public void Configure_result_for_last_specified_call()
|
||||
{
|
||||
var lastCallSpec = mock<ICallSpecification>();
|
||||
_getCallSpec.stub(x => x.FromLastCall(MatchArgs.AsSpecifiedInCall)).Return(lastCallSpec);
|
||||
|
||||
sut.SetResultForLastCall(_returnValue, MatchArgs.AsSpecifiedInCall);
|
||||
|
||||
_configuredResults.received(x => x.SetResult(lastCallSpec, _returnValue));
|
||||
}
|
||||
}
|
||||
|
||||
public class When_setting_result_for_a_call : Concern
|
||||
{
|
||||
[Test]
|
||||
public void Configure_result_with_new_spec_for_that_call()
|
||||
{
|
||||
var call = mock<ICall>();
|
||||
var callSpec = mock<ICallSpecification>();
|
||||
_getCallSpec.stub(x => x.FromCall(call, MatchArgs.AsSpecifiedInCall)).Return(callSpec);
|
||||
|
||||
sut.SetResultForCall(call, _returnValue, MatchArgs.AsSpecifiedInCall);
|
||||
|
||||
_configuredResults.received(x => x.SetResult(callSpec, _returnValue));
|
||||
}
|
||||
}
|
||||
|
||||
public class When_configuring_action_for_last_call : Concern
|
||||
{
|
||||
[Test]
|
||||
public void Configure_actions_for_last_specified_call()
|
||||
{
|
||||
Action<CallInfo> firstAction = x => { };
|
||||
Action<CallInfo> secondAction = x => { };
|
||||
var lastCallSpec = mock<ICallSpecification>();
|
||||
_getCallSpec.stub(x => x.FromLastCall(MatchArgs.AsSpecifiedInCall)).Return(lastCallSpec);
|
||||
|
||||
var config = sut.SetResultForLastCall(_returnValue, MatchArgs.AsSpecifiedInCall);
|
||||
config
|
||||
.AndDoes(firstAction)
|
||||
.AndDoes(secondAction);
|
||||
|
||||
_callActions.received(x => x.Add(lastCallSpec, firstAction));
|
||||
_callActions.received(x => x.Add(lastCallSpec, secondAction));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +1,41 @@
|
|||
using NSubstitute.Core;
|
||||
using NSubstitute.Core;
|
||||
using NSubstitute.Specs.Infrastructure;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace NSubstitute.Specs
|
||||
{
|
||||
public class ResultSetterSpecs
|
||||
public class GetCallSpecSpecs
|
||||
{
|
||||
public abstract class Concern : ConcernFor<ResultSetter>
|
||||
public abstract class Concern : ConcernFor<GetCallSpec>
|
||||
{
|
||||
protected ICallStack _callStack;
|
||||
protected IPendingSpecification _pendingSpecification;
|
||||
protected ICallActions _callActions;
|
||||
protected ICallResults _configuredResults;
|
||||
protected ICallSpecificationFactory _callSpecificationFactory;
|
||||
protected IReturn _returnValue;
|
||||
|
||||
public override void Context()
|
||||
{
|
||||
_callStack = mock<ICallStack>();
|
||||
_pendingSpecification = mock<IPendingSpecification>();
|
||||
_configuredResults = mock<ICallResults>();
|
||||
_callActions = mock<ICallActions>();
|
||||
_callSpecificationFactory = mock<ICallSpecificationFactory>();
|
||||
|
||||
_returnValue = mock<IReturn>();
|
||||
}
|
||||
|
||||
public override ResultSetter CreateSubjectUnderTest()
|
||||
public override GetCallSpec CreateSubjectUnderTest()
|
||||
{
|
||||
return new ResultSetter(_callStack, _pendingSpecification, _configuredResults, _callSpecificationFactory, _callActions);
|
||||
return new GetCallSpec(_callStack, _pendingSpecification, _callSpecificationFactory, _callActions);
|
||||
}
|
||||
}
|
||||
|
||||
public class When_a_call_specification_already_exists_for_the_last_call : Concern
|
||||
public class When_getting_for_last_call_with_pending_spec_and_matching_args : Concern
|
||||
{
|
||||
ICallSpecification _lastCallSpecification;
|
||||
ICallSpecification _result;
|
||||
|
||||
[Test]
|
||||
public void Should_configure_result_for_the_specification()
|
||||
public void Should_use_that_spec()
|
||||
{
|
||||
_configuredResults.received(x => x.SetResult(_lastCallSpecification, _returnValue));
|
||||
Assert.That(_result, Is.EqualTo(_lastCallSpecification));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -56,7 +52,7 @@ namespace NSubstitute.Specs
|
|||
|
||||
public override void Because()
|
||||
{
|
||||
sut.SetResultForLastCall(_returnValue, MatchArgs.AsSpecifiedInCall);
|
||||
_result = sut.FromLastCall(MatchArgs.AsSpecifiedInCall);
|
||||
}
|
||||
|
||||
public override void Context()
|
||||
|
@ -68,21 +64,22 @@ namespace NSubstitute.Specs
|
|||
}
|
||||
}
|
||||
|
||||
public class When_a_call_specification_already_exists_for_the_last_call_and_we_are_setting_for_any_args : Concern
|
||||
public class When_getting_for_last_call_with_pending_spec_and_setting_for_any_args : Concern
|
||||
{
|
||||
ICallSpecification _callSpecForAnyArgs;
|
||||
ICallSpecification _callSpecUpdatedForAnyArgs;
|
||||
ICallSpecification _lastCallSpecification;
|
||||
private ICallSpecification _result;
|
||||
|
||||
[Test]
|
||||
public void Should_configure_result_for_the_specification()
|
||||
public void Should_use_pending_call_spec_updated_to_match_any_args()
|
||||
{
|
||||
_configuredResults.received(x => x.SetResult(_callSpecForAnyArgs, _returnValue));
|
||||
Assert.That(_result, Is.EqualTo(_callSpecUpdatedForAnyArgs));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Should_move_actions_from_last_spec_to_spec_for_any_arguments()
|
||||
{
|
||||
_callActions.received(x => x.MoveActionsForSpecToNewSpec(_lastCallSpecification, _callSpecForAnyArgs));
|
||||
_callActions.received(x => x.MoveActionsForSpecToNewSpec(_lastCallSpecification, _callSpecUpdatedForAnyArgs));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -93,35 +90,36 @@ namespace NSubstitute.Specs
|
|||
|
||||
public override void Because()
|
||||
{
|
||||
sut.SetResultForLastCall(_returnValue, MatchArgs.Any);
|
||||
_result = sut.FromLastCall(MatchArgs.Any);
|
||||
}
|
||||
|
||||
public override void Context()
|
||||
{
|
||||
base.Context();
|
||||
_callSpecForAnyArgs = mock<ICallSpecification>();
|
||||
_callSpecUpdatedForAnyArgs = mock<ICallSpecification>();
|
||||
_lastCallSpecification = mock<ICallSpecification>();
|
||||
_pendingSpecification.stub(x => x.HasPendingCallSpec()).Return(true);
|
||||
_pendingSpecification.stub(x => x.UseCallSpec()).Return(_lastCallSpecification);
|
||||
_lastCallSpecification.stub(x => x.CreateCopyThatMatchesAnyArguments()).Return(_callSpecForAnyArgs);
|
||||
_lastCallSpecification.stub(x => x.CreateCopyThatMatchesAnyArguments()).Return(_callSpecUpdatedForAnyArgs);
|
||||
}
|
||||
}
|
||||
|
||||
public class When_setting_return_value_for_last_call_and_there_is_no_existing_call_spec : Concern
|
||||
public class When_getting_for_last_call_with_no_pending_call_spec : Concern
|
||||
{
|
||||
private readonly MatchArgs _argMatchStrategy = MatchArgs.AsSpecifiedInCall;
|
||||
readonly MatchArgs _argMatchStrategy = MatchArgs.AsSpecifiedInCall;
|
||||
ICall _call;
|
||||
ICallSpecification _callSpecification;
|
||||
ICallSpecification _result;
|
||||
|
||||
[Test]
|
||||
public void Should_remove_the_call_from_those_recorded_and_add_it_to_configured_results()
|
||||
{
|
||||
_configuredResults.received(x => x.SetResult(_callSpecification, _returnValue));
|
||||
Assert.That(_result, Is.EqualTo(_callSpecification));
|
||||
}
|
||||
|
||||
public override void Because()
|
||||
{
|
||||
sut.SetResultForLastCall(_returnValue, _argMatchStrategy);
|
||||
_result = sut.FromLastCall(_argMatchStrategy);
|
||||
}
|
||||
|
||||
public override void Context()
|
||||
|
@ -135,5 +133,6 @@ namespace NSubstitute.Specs
|
|||
_callSpecificationFactory.stub(x => x.CreateFrom(_call, _argMatchStrategy)).Return(_callSpecification);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -7,7 +7,10 @@ namespace NSubstitute.Specs.Infrastructure
|
|||
{
|
||||
public class TestCallRouter : ICallRouter
|
||||
{
|
||||
public void LastCallShouldReturn(IReturn returnValue, MatchArgs matchArgs) { }
|
||||
public ConfiguredCall LastCallShouldReturn(IReturn returnValue, MatchArgs matchArgs)
|
||||
{
|
||||
return new ConfiguredCall(x => { });
|
||||
}
|
||||
public void ClearReceivedCalls() { }
|
||||
public IEnumerable<ICall> ReceivedCalls() { return new ICall[0]; }
|
||||
|
||||
|
|
|
@ -94,6 +94,7 @@
|
|||
<Compile Include="CallFormatterSpecs.cs" />
|
||||
<Compile Include="CallInfoFactorySpecs.cs" />
|
||||
<Compile Include="CallInfoSpecs.cs" />
|
||||
<Compile Include="GetCallSpecSpecs.cs" />
|
||||
<Compile Include="Infrastructure\TestCallRouter.cs" />
|
||||
<Compile Include="QuantitySpecs.cs" />
|
||||
<Compile Include="ReceivedCallsExceptionThrowerSpecs.cs" />
|
||||
|
@ -144,7 +145,7 @@
|
|||
<Compile Include="Routing\Handlers\ReturnDefaultForReturnTypeHandlerSpecs.cs" />
|
||||
<Compile Include="ReturnExtensionSpec.cs" />
|
||||
<Compile Include="CallRouterSpecs.cs" />
|
||||
<Compile Include="ResultSetterSpecs.cs" />
|
||||
<Compile Include="ConfigureCallSpecs.cs" />
|
||||
<Compile Include="Routing\RouteSpecs.cs" />
|
||||
<Compile Include="Routing\Handlers\SetActionsForCallHandlerSpecs.cs" />
|
||||
<Compile Include="SequenceChecking\InstanceTrackerSpecs.cs" />
|
||||
|
|
|
@ -26,6 +26,7 @@ namespace NSubstitute.Specs
|
|||
_substitutionContext
|
||||
.stub(x => x.LastCallShouldReturn(It.IsAny<IReturn>(), It.IsAny<MatchArgs>()))
|
||||
.IgnoreArguments()
|
||||
.Return(new ConfiguredCall(x => { }))
|
||||
.WhenCalled(x => _returnValueSet = (IReturn) x.Arguments[0]);
|
||||
temporarilyChange(() => SubstitutionContext.Current).to(_substitutionContext);
|
||||
}
|
||||
|
|
|
@ -12,13 +12,13 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
protected readonly object _setValue = new object();
|
||||
protected ICall _call;
|
||||
protected IPropertyHelper _propertyHelper;
|
||||
protected IResultSetter _resultSetter;
|
||||
protected IConfigureCall ConfigureCall;
|
||||
protected ICall _propertyGetter;
|
||||
|
||||
public override void Context()
|
||||
{
|
||||
_propertyHelper = mock<IPropertyHelper>();
|
||||
_resultSetter = mock<IResultSetter>();
|
||||
ConfigureCall = mock<IConfigureCall>();
|
||||
_call = mock<ICall>();
|
||||
_propertyGetter = mock<ICall>();
|
||||
|
||||
|
@ -29,7 +29,7 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
|
||||
public override PropertySetterHandler CreateSubjectUnderTest()
|
||||
{
|
||||
return new PropertySetterHandler(_propertyHelper, _resultSetter);
|
||||
return new PropertySetterHandler(_propertyHelper, ConfigureCall);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,7 +59,7 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
{
|
||||
base.Context();
|
||||
_propertyHelper.stub(x => x.IsCallToSetAReadWriteProperty(_call)).Return(true);
|
||||
_resultSetter
|
||||
ConfigureCall
|
||||
.stub(x => x.SetResultForCall(It.Is(_propertyGetter), It.IsAny<IReturn>(), It.Is(MatchArgs.AsSpecifiedInCall)))
|
||||
.WhenCalled(x => _returnPassedToResultSetter = (ReturnValue)x.Arguments[1]);
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
[Test]
|
||||
public void Should_not_add_any_values_to_configured_results()
|
||||
{
|
||||
_resultSetter.did_not_receive(x => x.SetResultForCall(It.Is(_propertyGetter), It.IsAny<IReturn>(), It.Is(MatchArgs.AsSpecifiedInCall)));
|
||||
ConfigureCall.did_not_receive(x => x.SetResultForCall(It.Is(_propertyGetter), It.IsAny<IReturn>(), It.Is(MatchArgs.AsSpecifiedInCall)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
protected IAutoValueProvider _secondAutoValueProvider;
|
||||
protected RouteAction _result;
|
||||
protected ICall _call;
|
||||
protected IResultSetter _resultSetter;
|
||||
protected IConfigureCall ConfigureCall;
|
||||
|
||||
public override void Because()
|
||||
{
|
||||
|
@ -26,7 +26,7 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
public override void Context()
|
||||
{
|
||||
base.Context();
|
||||
_resultSetter = mock<IResultSetter>();
|
||||
ConfigureCall = mock<IConfigureCall>();
|
||||
_call = mock<ICall>();
|
||||
_call.stub(x => x.GetReturnType()).Return(_type);
|
||||
_firstAutoValueProvider = mock<IAutoValueProvider>();
|
||||
|
@ -35,7 +35,7 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
|
||||
public override ReturnAutoValueForThisAndSubsequentCallsHandler CreateSubjectUnderTest()
|
||||
{
|
||||
return new ReturnAutoValueForThisAndSubsequentCallsHandler(new[] {_firstAutoValueProvider, _secondAutoValueProvider}, _resultSetter);
|
||||
return new ReturnAutoValueForThisAndSubsequentCallsHandler(new[] {_firstAutoValueProvider, _secondAutoValueProvider}, ConfigureCall);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ namespace NSubstitute.Specs.Routing.Handlers
|
|||
[Test]
|
||||
public void Should_set_auto_value_as_value_to_return_for_subsequent_calls()
|
||||
{
|
||||
_resultSetter.received(x => x.SetResultForCall(It.Is(_call), It.Matches<IReturn>(arg => arg.ReturnFor(null) == _autoValue), It.Is(MatchArgs.AsSpecifiedInCall)));
|
||||
ConfigureCall.received(x => x.SetResultForCall(It.Is(_call), It.Matches<IReturn>(arg => arg.ReturnFor(null) == _autoValue), It.Is(MatchArgs.AsSpecifiedInCall)));
|
||||
}
|
||||
|
||||
public override void Context()
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace NSubstitute.Core
|
|||
readonly ISubstituteState _substituteState;
|
||||
readonly ISubstitutionContext _context;
|
||||
readonly IReceivedCalls _receivedCalls;
|
||||
readonly IResultSetter _resultSetter;
|
||||
readonly IConfigureCall ConfigureCall;
|
||||
readonly IRouteFactory _routeFactory;
|
||||
IRoute _currentRoute;
|
||||
bool _isSetToDefaultRoute;
|
||||
|
@ -24,7 +24,7 @@ namespace NSubstitute.Core
|
|||
_context = context;
|
||||
_routeFactory = routeFactory;
|
||||
_receivedCalls = substituteState.ReceivedCalls;
|
||||
_resultSetter = substituteState.ResultSetter;
|
||||
ConfigureCall = substituteState.ConfigureCall;
|
||||
|
||||
UseDefaultRouteForNextCall();
|
||||
}
|
||||
|
@ -57,9 +57,9 @@ namespace NSubstitute.Core
|
|||
return routeToUseForThisCall.Handle(call);
|
||||
}
|
||||
|
||||
public void LastCallShouldReturn(IReturn returnValue, MatchArgs matchArgs)
|
||||
public ConfiguredCall LastCallShouldReturn(IReturn returnValue, MatchArgs matchArgs)
|
||||
{
|
||||
_resultSetter.SetResultForLastCall(returnValue, matchArgs);
|
||||
return ConfigureCall.SetResultForLastCall(returnValue, matchArgs);
|
||||
}
|
||||
|
||||
private bool IsSpecifyingACall(ICall call)
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
using NSubstitute.Core.Arguments;
|
||||
|
||||
namespace NSubstitute.Core
|
||||
{
|
||||
public class CallSpecificationFactoryFactoryYesThatsRight
|
||||
{
|
||||
public static ICallSpecificationFactory CreateCallSpecFactory()
|
||||
{
|
||||
return
|
||||
new CallSpecificationFactory(
|
||||
new ArgumentSpecificationsFactory(
|
||||
new MixedArgumentSpecificationsFactory(
|
||||
new ArgumentSpecificationFactory(
|
||||
NewParamsArgumentSpecificationFactory(),
|
||||
NewNonParamsArgumentSpecificationFactory()
|
||||
),
|
||||
new SuppliedArgumentSpecificationsFactory(NewDefaultChecker())
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private static IDefaultChecker NewDefaultChecker()
|
||||
{
|
||||
return new DefaultChecker(new DefaultForType());
|
||||
}
|
||||
|
||||
private static IParamsArgumentSpecificationFactory NewParamsArgumentSpecificationFactory()
|
||||
{
|
||||
return
|
||||
new ParamsArgumentSpecificationFactory(
|
||||
NewDefaultChecker(),
|
||||
new ArgumentEqualsSpecificationFactory(),
|
||||
new ArrayArgumentSpecificationsFactory(
|
||||
new NonParamsArgumentSpecificationFactory(new ArgumentEqualsSpecificationFactory())
|
||||
),
|
||||
new ParameterInfosFromParamsArrayFactory(),
|
||||
new SuppliedArgumentSpecificationsFactory(NewDefaultChecker()),
|
||||
new ArrayContentsArgumentSpecificationFactory()
|
||||
);
|
||||
}
|
||||
|
||||
private static INonParamsArgumentSpecificationFactory NewNonParamsArgumentSpecificationFactory()
|
||||
{
|
||||
return
|
||||
new NonParamsArgumentSpecificationFactory(new ArgumentEqualsSpecificationFactory()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
|
||||
namespace NSubstitute.Core
|
||||
{
|
||||
public class ConfigureCall : IConfigureCall
|
||||
{
|
||||
private readonly ICallResults _configuredResults;
|
||||
private readonly ICallActions _callActions;
|
||||
private readonly IGetCallSpec _getCallSpec;
|
||||
|
||||
public ConfigureCall(ICallResults configuredResults, ICallActions callActions, IGetCallSpec getCallSpec)
|
||||
{
|
||||
_configuredResults = configuredResults;
|
||||
_callActions = callActions;
|
||||
_getCallSpec = getCallSpec;
|
||||
}
|
||||
|
||||
public ConfiguredCall SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs)
|
||||
{
|
||||
var spec = _getCallSpec.FromLastCall(matchArgs);
|
||||
_configuredResults.SetResult(spec, valueToReturn);
|
||||
return new ConfiguredCall(action => _callActions.Add(spec, action));
|
||||
}
|
||||
|
||||
public void SetResultForCall(ICall call, IReturn valueToReturn, MatchArgs matchArgs)
|
||||
{
|
||||
var callSpecification = _getCallSpec.FromCall(call, matchArgs);
|
||||
_configuredResults.SetResult(callSpecification, valueToReturn);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
|
||||
namespace NSubstitute.Core
|
||||
{
|
||||
public class ConfiguredCall
|
||||
{
|
||||
private readonly Action<Action<CallInfo>> _addAction;
|
||||
|
||||
public ConfiguredCall(Action<Action<CallInfo>> addAction)
|
||||
{
|
||||
_addAction = addAction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a callback to execute for matching calls.
|
||||
/// </summary>
|
||||
/// <param name="action">an action to call</param>
|
||||
/// <returns></returns>
|
||||
public ConfiguredCall AndDoes(Action<CallInfo> action)
|
||||
{
|
||||
_addAction(action);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
namespace NSubstitute.Core
|
||||
{
|
||||
public class GetCallSpec : IGetCallSpec
|
||||
{
|
||||
private readonly ICallStack _callStack;
|
||||
private readonly IPendingSpecification _pendingSpecification;
|
||||
private readonly ICallSpecificationFactory _callSpecificationFactory;
|
||||
private readonly ICallActions _callActions;
|
||||
|
||||
public GetCallSpec(ICallStack callStack, IPendingSpecification pendingSpecification,
|
||||
ICallSpecificationFactory callSpecificationFactory, ICallActions callActions)
|
||||
{
|
||||
_callStack = callStack;
|
||||
_pendingSpecification = pendingSpecification;
|
||||
_callSpecificationFactory = callSpecificationFactory;
|
||||
_callActions = callActions;
|
||||
}
|
||||
|
||||
public ICallSpecification FromLastCall(MatchArgs matchArgs)
|
||||
{
|
||||
return _pendingSpecification.HasPendingCallSpec()
|
||||
? FromExistingSpec(_pendingSpecification.UseCallSpec(), matchArgs)
|
||||
: FromCall(_callStack.Pop(), matchArgs);
|
||||
}
|
||||
|
||||
public ICallSpecification FromCall(ICall call, MatchArgs matchArgs)
|
||||
{
|
||||
return _callSpecificationFactory.CreateFrom(call, matchArgs);
|
||||
}
|
||||
|
||||
public ICallSpecification FromExistingSpec(ICallSpecification spec, MatchArgs matchArgs)
|
||||
{
|
||||
return matchArgs == MatchArgs.AsSpecifiedInCall ? spec : UpdateCallSpecToMatchAnyArgs(spec);
|
||||
}
|
||||
|
||||
ICallSpecification UpdateCallSpecToMatchAnyArgs(ICallSpecification callSpecification)
|
||||
{
|
||||
var anyArgCallSpec = callSpecification.CreateCopyThatMatchesAnyArguments();
|
||||
_callActions.MoveActionsForSpecToNewSpec(callSpecification, anyArgCallSpec);
|
||||
return anyArgCallSpec;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ namespace NSubstitute.Core
|
|||
{
|
||||
public interface ICallRouter
|
||||
{
|
||||
void LastCallShouldReturn(IReturn returnValue, MatchArgs matchArgs);
|
||||
ConfiguredCall LastCallShouldReturn(IReturn returnValue, MatchArgs matchArgs);
|
||||
object Route(ICall call);
|
||||
void ClearReceivedCalls();
|
||||
IEnumerable<ICall> ReceivedCalls();
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
namespace NSubstitute.Core
|
||||
{
|
||||
public interface IResultSetter
|
||||
public interface IConfigureCall
|
||||
{
|
||||
void SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs);
|
||||
ConfiguredCall SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs);
|
||||
void SetResultForCall(ICall call, IReturn valueToReturn, MatchArgs matchArgs);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace NSubstitute.Core
|
||||
{
|
||||
public interface IGetCallSpec
|
||||
{
|
||||
ICallSpecification FromLastCall(MatchArgs matchArgs);
|
||||
ICallSpecification FromExistingSpec(ICallSpecification spec, MatchArgs matchArgs);
|
||||
ICallSpecification FromCall(ICall call, MatchArgs matchArgs);
|
||||
}
|
||||
}
|
|
@ -6,20 +6,16 @@ namespace NSubstitute.Core
|
|||
public interface ISubstituteState
|
||||
{
|
||||
ISubstitutionContext SubstitutionContext { get; }
|
||||
ICallInfoFactory CallInfoFactory { get; }
|
||||
ICallStack CallStack { get; }
|
||||
IReceivedCalls ReceivedCalls { get; }
|
||||
IPendingSpecification PendingSpecification { get; }
|
||||
ICallResults CallResults { get; }
|
||||
ICallSpecificationFactory CallSpecificationFactory { get; }
|
||||
ISubstituteFactory SubstituteFactory { get; }
|
||||
ICallActions CallActions { get; }
|
||||
SequenceNumberGenerator SequenceNumberGenerator { get; }
|
||||
IPropertyHelper PropertyHelper { get; }
|
||||
IResultSetter ResultSetter { get; }
|
||||
IConfigureCall ConfigureCall { get; }
|
||||
IEventHandlerRegistry EventHandlerRegistry { get; }
|
||||
IReceivedCallsExceptionThrower ReceivedCallsExceptionThrower { get; }
|
||||
IDefaultForType DefaultForType { get; }
|
||||
IAutoValueProvider[] AutoValueProviders { get; }
|
||||
void ClearUnusedCallSpecs();
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ namespace NSubstitute.Core
|
|||
{
|
||||
ISubstituteFactory SubstituteFactory { get; }
|
||||
SequenceNumberGenerator SequenceNumberGenerator { get; }
|
||||
void LastCallShouldReturn(IReturn value, MatchArgs matchArgs);
|
||||
ConfiguredCall LastCallShouldReturn(IReturn value, MatchArgs matchArgs);
|
||||
void LastCallRouter(ICallRouter callRouter);
|
||||
ICallRouter GetCallRouterFor(object substitute);
|
||||
void EnqueueArgumentSpecification(IArgumentSpecification spec);
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
namespace NSubstitute.Core
|
||||
{
|
||||
public class ResultSetter : IResultSetter
|
||||
{
|
||||
private readonly ICallStack _callStack;
|
||||
private readonly IPendingSpecification _pendingSpecification;
|
||||
private readonly ICallResults _configuredResults;
|
||||
private readonly ICallSpecificationFactory _callSpecificationFactory;
|
||||
private readonly ICallActions _callActions;
|
||||
|
||||
public ResultSetter(ICallStack callStack, IPendingSpecification pendingSpecification,
|
||||
ICallResults configuredResults, ICallSpecificationFactory callSpecificationFactory,
|
||||
ICallActions callActions)
|
||||
{
|
||||
_callStack = callStack;
|
||||
_pendingSpecification = pendingSpecification;
|
||||
_configuredResults = configuredResults;
|
||||
_callSpecificationFactory = callSpecificationFactory;
|
||||
_callActions = callActions;
|
||||
}
|
||||
|
||||
public void SetResultForLastCall(IReturn valueToReturn, MatchArgs matchArgs)
|
||||
{
|
||||
if (_pendingSpecification.HasPendingCallSpec())
|
||||
{
|
||||
SetResultForCall(_pendingSpecification.UseCallSpec(), valueToReturn, matchArgs);
|
||||
}
|
||||
else
|
||||
{
|
||||
var lastCall = _callStack.Pop();
|
||||
SetResultForCall(lastCall, valueToReturn, matchArgs);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetResultForCall(ICall call, IReturn valueToReturn, MatchArgs matchArgs)
|
||||
{
|
||||
var callSpecification = _callSpecificationFactory.CreateFrom(call, matchArgs);
|
||||
_configuredResults.SetResult(callSpecification, valueToReturn);
|
||||
}
|
||||
|
||||
void SetResultForCall(ICallSpecification callSpecification, IReturn valueToReturn, MatchArgs matchArgs)
|
||||
{
|
||||
var callSpecForReturnValue = GetCallSpecForArgMatchStrategy(callSpecification, matchArgs);
|
||||
_configuredResults.SetResult(callSpecForReturnValue, valueToReturn);
|
||||
}
|
||||
|
||||
ICallSpecification GetCallSpecForArgMatchStrategy(ICallSpecification callSpecification, MatchArgs matchArgs)
|
||||
{
|
||||
return matchArgs == MatchArgs.AsSpecifiedInCall ? callSpecification : UpdateCallSpecToMatchAnyArgs(callSpecification);
|
||||
}
|
||||
|
||||
ICallSpecification UpdateCallSpecToMatchAnyArgs(ICallSpecification callSpecification)
|
||||
{
|
||||
var anyArgCallSpec = callSpecification.CreateCopyThatMatchesAnyArguments();
|
||||
_callActions.MoveActionsForSpecToNewSpec(callSpecification, anyArgCallSpec);
|
||||
return anyArgCallSpec;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,48 +1,41 @@
|
|||
using NSubstitute.Routing.AutoValues;
|
||||
using NSubstitute.Core.Arguments;
|
||||
|
||||
namespace NSubstitute.Core
|
||||
{
|
||||
public class SubstituteState : ISubstituteState
|
||||
{
|
||||
public ISubstitutionContext SubstitutionContext { get; private set; }
|
||||
public ICallInfoFactory CallInfoFactory { get; private set; }
|
||||
public ICallStack CallStack { get; private set; }
|
||||
public IReceivedCalls ReceivedCalls { get; private set; }
|
||||
public IPendingSpecification PendingSpecification { get; private set; }
|
||||
public ICallResults CallResults { get; private set; }
|
||||
public ICallSpecificationFactory CallSpecificationFactory { get; private set; }
|
||||
public ISubstituteFactory SubstituteFactory { get; private set; }
|
||||
public ICallActions CallActions { get; private set; }
|
||||
public SequenceNumberGenerator SequenceNumberGenerator { get; private set; }
|
||||
public IPropertyHelper PropertyHelper { get; private set; }
|
||||
public IResultSetter ResultSetter { get; private set; }
|
||||
public IConfigureCall ConfigureCall { get; private set; }
|
||||
public IEventHandlerRegistry EventHandlerRegistry { get; private set; }
|
||||
public IReceivedCallsExceptionThrower ReceivedCallsExceptionThrower { get; private set; }
|
||||
public IDefaultForType DefaultForType { get; private set; }
|
||||
public IAutoValueProvider[] AutoValueProviders { get; private set; }
|
||||
|
||||
public SubstituteState(ISubstitutionContext substitutionContext)
|
||||
{
|
||||
SubstitutionContext = substitutionContext;
|
||||
SubstituteFactory = substitutionContext.SubstituteFactory;
|
||||
SequenceNumberGenerator = substitutionContext.SequenceNumberGenerator;
|
||||
CallInfoFactory = new CallInfoFactory();
|
||||
var substituteFactory = substitutionContext.SubstituteFactory;
|
||||
var callInfoFactory = new CallInfoFactory();
|
||||
var callStack = new CallStack();
|
||||
CallStack = callStack;
|
||||
ReceivedCalls = callStack;
|
||||
PendingSpecification = new PendingSpecification();
|
||||
CallResults = new CallResults(CallInfoFactory);
|
||||
CallSpecificationFactory = NewCallSpecificationFactory();
|
||||
CallActions = new CallActions(CallInfoFactory);
|
||||
CallResults = new CallResults(callInfoFactory);
|
||||
CallSpecificationFactory = CallSpecificationFactoryFactoryYesThatsRight.CreateCallSpecFactory();
|
||||
CallActions = new CallActions(callInfoFactory);
|
||||
|
||||
PropertyHelper = new PropertyHelper();
|
||||
ResultSetter = new ResultSetter(CallStack, PendingSpecification, CallResults, CallSpecificationFactory, CallActions);
|
||||
var getCallSpec = new GetCallSpec(callStack, PendingSpecification, CallSpecificationFactory, CallActions);
|
||||
|
||||
ConfigureCall = new ConfigureCall(CallResults, CallActions, getCallSpec);
|
||||
EventHandlerRegistry = new EventHandlerRegistry();
|
||||
ReceivedCallsExceptionThrower = new ReceivedCallsExceptionThrower();
|
||||
DefaultForType = new DefaultForType();
|
||||
AutoValueProviders = new IAutoValueProvider[] {
|
||||
new AutoSubstituteProvider(SubstituteFactory),
|
||||
new AutoSubstituteProvider(substituteFactory),
|
||||
new AutoStringProvider(),
|
||||
new AutoArrayProvider(),
|
||||
#if NET4
|
||||
|
@ -51,48 +44,10 @@ namespace NSubstitute.Core
|
|||
};
|
||||
}
|
||||
|
||||
private static IDefaultChecker NewDefaultChecker()
|
||||
public void ClearUnusedCallSpecs()
|
||||
{
|
||||
return new DefaultChecker(new DefaultForType());
|
||||
PendingSpecification.Clear();
|
||||
}
|
||||
|
||||
private static IParamsArgumentSpecificationFactory NewParamsArgumentSpecificationFactory()
|
||||
{
|
||||
return
|
||||
new ParamsArgumentSpecificationFactory(
|
||||
NewDefaultChecker(),
|
||||
new ArgumentEqualsSpecificationFactory(),
|
||||
new ArrayArgumentSpecificationsFactory(
|
||||
new NonParamsArgumentSpecificationFactory(new ArgumentEqualsSpecificationFactory()
|
||||
)
|
||||
),
|
||||
new ParameterInfosFromParamsArrayFactory(),
|
||||
new SuppliedArgumentSpecificationsFactory(NewDefaultChecker()),
|
||||
new ArrayContentsArgumentSpecificationFactory()
|
||||
);
|
||||
}
|
||||
|
||||
private static INonParamsArgumentSpecificationFactory NewNonParamsArgumentSpecificationFactory()
|
||||
{
|
||||
return
|
||||
new NonParamsArgumentSpecificationFactory(new ArgumentEqualsSpecificationFactory()
|
||||
);
|
||||
}
|
||||
|
||||
private static ICallSpecificationFactory NewCallSpecificationFactory()
|
||||
{
|
||||
return
|
||||
new CallSpecificationFactory(
|
||||
new ArgumentSpecificationsFactory(
|
||||
new MixedArgumentSpecificationsFactory(
|
||||
new ArgumentSpecificationFactory(
|
||||
NewParamsArgumentSpecificationFactory(),
|
||||
NewNonParamsArgumentSpecificationFactory()
|
||||
),
|
||||
new SuppliedArgumentSpecificationsFactory(NewDefaultChecker())
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,11 +44,12 @@ namespace NSubstitute.Core
|
|||
public SequenceNumberGenerator SequenceNumberGenerator { get { return _sequenceNumberGenerator; } }
|
||||
public bool IsQuerying { get { return _currentQuery.Value != null; } }
|
||||
|
||||
public void LastCallShouldReturn(IReturn value, MatchArgs matchArgs)
|
||||
public ConfiguredCall LastCallShouldReturn(IReturn value, MatchArgs matchArgs)
|
||||
{
|
||||
if (_lastCallRouter.Value == null) throw new CouldNotSetReturnException();
|
||||
_lastCallRouter.Value.LastCallShouldReturn(value, matchArgs);
|
||||
var configuredCall = _lastCallRouter.Value.LastCallShouldReturn(value, matchArgs);
|
||||
ClearLastCallRouter();
|
||||
return configuredCall;
|
||||
}
|
||||
|
||||
public void ClearLastCallRouter()
|
||||
|
|
|
@ -117,11 +117,15 @@
|
|||
<Compile Include="Core\Arguments\SuppliedArgumentSpecificationsFactory.cs" />
|
||||
<Compile Include="Core\CallFactory.cs" />
|
||||
<Compile Include="Core\CallSpecAndTarget.cs" />
|
||||
<Compile Include="Core\CallSpecificationFactoryFactoryYesThatsRight.cs" />
|
||||
<Compile Include="Core\ConfiguredCall.cs" />
|
||||
<Compile Include="Core\DefaultForType.cs" />
|
||||
<Compile Include="Core\EventCallFormatter.cs" />
|
||||
<Compile Include="Core\Arguments\IArgumentEqualsSpecificationFactory.cs" />
|
||||
<Compile Include="Core\Extensions.cs" />
|
||||
<Compile Include="Core\GetCallSpec.cs" />
|
||||
<Compile Include="Core\ICallRouterProvider.cs" />
|
||||
<Compile Include="Core\IGetCallSpec.cs" />
|
||||
<Compile Include="Core\SequenceChecking\InstanceTracker.cs" />
|
||||
<Compile Include="Core\SequenceChecking\SequenceFormatter.cs" />
|
||||
<Compile Include="Core\IParameterInfo.cs" />
|
||||
|
@ -217,7 +221,7 @@
|
|||
<Compile Include="Core\IEventRaiser.cs" />
|
||||
<Compile Include="Core\IProxyFactory.cs" />
|
||||
<Compile Include="Core\IPropertyHelper.cs" />
|
||||
<Compile Include="Core\IResultSetter.cs" />
|
||||
<Compile Include="Core\IConfigureCall.cs" />
|
||||
<Compile Include="Routing\IRoute.cs" />
|
||||
<Compile Include="Core\ISubstitutionContext.cs" />
|
||||
<Compile Include="Core\ISubstituteFactory.cs" />
|
||||
|
@ -231,7 +235,7 @@
|
|||
<Compile Include="Routing\Handlers\ReturnConfiguredResultHandler.cs" />
|
||||
<Compile Include="Routing\Handlers\RecordCallHandler.cs" />
|
||||
<Compile Include="Core\PropertyHelper.cs" />
|
||||
<Compile Include="Core\ResultSetter.cs" />
|
||||
<Compile Include="Core\ConfigureCall.cs" />
|
||||
<Compile Include="Routing\Handlers\ReturnDefaultForReturnTypeHandler.cs" />
|
||||
<Compile Include="Routing\Handlers\SetActionForCallHandler.cs" />
|
||||
<Compile Include="Routing\RouteFactory.cs" />
|
||||
|
|
|
@ -4,16 +4,16 @@ namespace NSubstitute.Routing.Handlers
|
|||
{
|
||||
public class ClearUnusedCallSpecHandler : ICallHandler
|
||||
{
|
||||
private readonly IPendingSpecification _pendingSpecification;
|
||||
private readonly ISubstituteState _state;
|
||||
|
||||
public ClearUnusedCallSpecHandler(IPendingSpecification pendingSpecification)
|
||||
public ClearUnusedCallSpecHandler(ISubstituteState state)
|
||||
{
|
||||
_pendingSpecification = pendingSpecification;
|
||||
_state = state;
|
||||
}
|
||||
|
||||
public RouteAction Handle(ICall call)
|
||||
{
|
||||
_pendingSpecification.Clear();
|
||||
_state.ClearUnusedCallSpecs();
|
||||
return RouteAction.Continue();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,12 +6,12 @@ namespace NSubstitute.Routing.Handlers
|
|||
public class PropertySetterHandler : ICallHandler
|
||||
{
|
||||
private readonly IPropertyHelper _propertyHelper;
|
||||
readonly IResultSetter _resultSetter;
|
||||
readonly IConfigureCall ConfigureCall;
|
||||
|
||||
public PropertySetterHandler(IPropertyHelper propertyHelper, IResultSetter resultSetter)
|
||||
public PropertySetterHandler(IPropertyHelper propertyHelper, IConfigureCall configureCall)
|
||||
{
|
||||
_propertyHelper = propertyHelper;
|
||||
_resultSetter = resultSetter;
|
||||
ConfigureCall = configureCall;
|
||||
}
|
||||
|
||||
public RouteAction Handle(ICall call)
|
||||
|
@ -20,7 +20,7 @@ namespace NSubstitute.Routing.Handlers
|
|||
{
|
||||
var callToPropertyGetter = _propertyHelper.CreateCallToPropertyGetterFromSetterCall(call);
|
||||
var valueBeingSetOnProperty = call.GetArguments().Last();
|
||||
_resultSetter.SetResultForCall(callToPropertyGetter, new ReturnValue(valueBeingSetOnProperty), MatchArgs.AsSpecifiedInCall);
|
||||
ConfigureCall.SetResultForCall(callToPropertyGetter, new ReturnValue(valueBeingSetOnProperty), MatchArgs.AsSpecifiedInCall);
|
||||
}
|
||||
return RouteAction.Continue();
|
||||
}
|
||||
|
|
|
@ -9,12 +9,12 @@ namespace NSubstitute.Routing.Handlers
|
|||
public class ReturnAutoValueForThisAndSubsequentCallsHandler : ICallHandler
|
||||
{
|
||||
private readonly IEnumerable<IAutoValueProvider> _autoValueProviders;
|
||||
private readonly IResultSetter _resultSetter;
|
||||
private readonly IConfigureCall ConfigureCall;
|
||||
|
||||
public ReturnAutoValueForThisAndSubsequentCallsHandler(IEnumerable<IAutoValueProvider> autoValueProviders, IResultSetter resultSetter)
|
||||
public ReturnAutoValueForThisAndSubsequentCallsHandler(IEnumerable<IAutoValueProvider> autoValueProviders, IConfigureCall configureCall)
|
||||
{
|
||||
_autoValueProviders = autoValueProviders;
|
||||
_resultSetter = resultSetter;
|
||||
ConfigureCall = configureCall;
|
||||
}
|
||||
|
||||
public RouteAction Handle(ICall call)
|
||||
|
@ -24,7 +24,7 @@ namespace NSubstitute.Routing.Handlers
|
|||
if (compatibleProviders.Any())
|
||||
{
|
||||
var valueToReturn = compatibleProviders.First().GetValue(type);
|
||||
_resultSetter.SetResultForCall(call, new ReturnValue(valueToReturn), MatchArgs.AsSpecifiedInCall);
|
||||
ConfigureCall.SetResultForCall(call, new ReturnValue(valueToReturn), MatchArgs.AsSpecifiedInCall);
|
||||
return RouteAction.Return(valueToReturn);
|
||||
}
|
||||
return RouteAction.Continue();
|
||||
|
|
|
@ -9,62 +9,67 @@ namespace NSubstitute.Routing
|
|||
public IRoute CallQuery(ISubstituteState state)
|
||||
{
|
||||
return new Route(new ICallHandler[] {
|
||||
new ClearUnusedCallSpecHandler(state.PendingSpecification)
|
||||
new ClearUnusedCallSpecHandler(state)
|
||||
, new AddCallToQueryResultHandler(state.SubstitutionContext, state.CallSpecificationFactory)
|
||||
, new ReturnConfiguredResultHandler(state.CallResults)
|
||||
, new ReturnAutoValueForThisAndSubsequentCallsHandler(state.AutoValueProviders, state.ResultSetter)
|
||||
, new ReturnDefaultForReturnTypeHandler(state.DefaultForType)
|
||||
, new ReturnAutoValueForThisAndSubsequentCallsHandler(state.AutoValueProviders, state.ConfigureCall)
|
||||
, ReturnDefaultForReturnTypeHandler()
|
||||
});
|
||||
}
|
||||
public IRoute CheckReceivedCalls(ISubstituteState state, MatchArgs matchArgs, Quantity requiredQuantity)
|
||||
{
|
||||
return new Route(new ICallHandler[] {
|
||||
new ClearLastCallRouterHandler(state.SubstitutionContext)
|
||||
, new ClearUnusedCallSpecHandler(state.PendingSpecification)
|
||||
, new CheckReceivedCallsHandler(state.ReceivedCalls, state.CallSpecificationFactory, state.ReceivedCallsExceptionThrower, matchArgs, requiredQuantity)
|
||||
, new ReturnDefaultForReturnTypeHandler(state.DefaultForType)
|
||||
, new ClearUnusedCallSpecHandler(state)
|
||||
, new CheckReceivedCallsHandler(state.ReceivedCalls, state.CallSpecificationFactory, new ReceivedCallsExceptionThrower(), matchArgs, requiredQuantity)
|
||||
, ReturnDefaultForReturnTypeHandler()
|
||||
});
|
||||
}
|
||||
public IRoute DoWhenCalled(ISubstituteState state, Action<CallInfo> doAction, MatchArgs matchArgs)
|
||||
{
|
||||
return new Route(new ICallHandler[] {
|
||||
new ClearLastCallRouterHandler(state.SubstitutionContext)
|
||||
, new ClearUnusedCallSpecHandler(state.PendingSpecification)
|
||||
, new ClearUnusedCallSpecHandler(state)
|
||||
, new SetActionForCallHandler(state.CallSpecificationFactory, state.CallActions, doAction, matchArgs)
|
||||
, new ReturnDefaultForReturnTypeHandler(state.DefaultForType)
|
||||
, ReturnDefaultForReturnTypeHandler()
|
||||
});
|
||||
}
|
||||
public IRoute RaiseEvent(ISubstituteState state, Func<ICall, object[]> getEventArguments)
|
||||
{
|
||||
return new Route(new ICallHandler[] {
|
||||
new ClearLastCallRouterHandler(state.SubstitutionContext)
|
||||
, new ClearUnusedCallSpecHandler(state.PendingSpecification)
|
||||
, new ClearUnusedCallSpecHandler(state)
|
||||
, new RaiseEventHandler(state.EventHandlerRegistry, getEventArguments)
|
||||
, new ReturnDefaultForReturnTypeHandler(state.DefaultForType)
|
||||
, ReturnDefaultForReturnTypeHandler()
|
||||
});
|
||||
}
|
||||
public IRoute RecordCallSpecification(ISubstituteState state)
|
||||
{
|
||||
return new Route(new ICallHandler[] {
|
||||
new RecordCallSpecificationHandler(state.PendingSpecification, state.CallSpecificationFactory, state.CallActions)
|
||||
, new PropertySetterHandler(state.PropertyHelper, state.ResultSetter)
|
||||
, new PropertySetterHandler(new PropertyHelper(), state.ConfigureCall)
|
||||
, new ReturnConfiguredResultHandler(state.CallResults)
|
||||
, new ReturnAutoValueForThisAndSubsequentCallsHandler(state.AutoValueProviders, state.ResultSetter)
|
||||
, new ReturnDefaultForReturnTypeHandler(state.DefaultForType)
|
||||
, new ReturnAutoValueForThisAndSubsequentCallsHandler(state.AutoValueProviders, state.ConfigureCall)
|
||||
, ReturnDefaultForReturnTypeHandler()
|
||||
});
|
||||
}
|
||||
public IRoute RecordReplay(ISubstituteState state)
|
||||
{
|
||||
return new Route(RouteType.RecordReplay, new ICallHandler[] {
|
||||
new ClearUnusedCallSpecHandler(state.PendingSpecification)
|
||||
new ClearUnusedCallSpecHandler(state)
|
||||
, new RecordCallHandler(state.CallStack, state.SequenceNumberGenerator)
|
||||
, new EventSubscriptionHandler(state.EventHandlerRegistry)
|
||||
, new PropertySetterHandler(state.PropertyHelper, state.ResultSetter)
|
||||
, new PropertySetterHandler(new PropertyHelper(), state.ConfigureCall)
|
||||
, new DoActionsCallHandler(state.CallActions)
|
||||
, new ReturnConfiguredResultHandler(state.CallResults)
|
||||
, new ReturnAutoValueForThisAndSubsequentCallsHandler(state.AutoValueProviders, state.ResultSetter)
|
||||
, new ReturnDefaultForReturnTypeHandler(state.DefaultForType)
|
||||
, new ReturnAutoValueForThisAndSubsequentCallsHandler(state.AutoValueProviders, state.ConfigureCall)
|
||||
, ReturnDefaultForReturnTypeHandler()
|
||||
});
|
||||
}
|
||||
|
||||
private static ReturnDefaultForReturnTypeHandler ReturnDefaultForReturnTypeHandler()
|
||||
{
|
||||
return new ReturnDefaultForReturnTypeHandler(new DefaultForType());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,27 +8,27 @@ namespace NSubstitute
|
|||
{
|
||||
public static class SubstituteExtensions
|
||||
{
|
||||
public static void Returns<T>(this T value, T returnThis, params T[] returnThese)
|
||||
public static ConfiguredCall Returns<T>(this T value, T returnThis, params T[] returnThese)
|
||||
{
|
||||
Returns(MatchArgs.AsSpecifiedInCall, returnThis, returnThese);
|
||||
return Returns(MatchArgs.AsSpecifiedInCall, returnThis, returnThese);
|
||||
}
|
||||
|
||||
public static void Returns<T>(this T value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)
|
||||
public static ConfiguredCall Returns<T>(this T value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)
|
||||
{
|
||||
Returns(MatchArgs.AsSpecifiedInCall, returnThis, returnThese);
|
||||
return Returns(MatchArgs.AsSpecifiedInCall, returnThis, returnThese);
|
||||
}
|
||||
|
||||
public static void ReturnsForAnyArgs<T>(this T value, T returnThis, params T[] returnThese)
|
||||
public static ConfiguredCall ReturnsForAnyArgs<T>(this T value, T returnThis, params T[] returnThese)
|
||||
{
|
||||
Returns(MatchArgs.Any, returnThis, returnThese);
|
||||
return Returns(MatchArgs.Any, returnThis, returnThese);
|
||||
}
|
||||
|
||||
public static void ReturnsForAnyArgs<T>(this T value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)
|
||||
public static ConfiguredCall ReturnsForAnyArgs<T>(this T value, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)
|
||||
{
|
||||
Returns(MatchArgs.Any, returnThis, returnThese);
|
||||
return Returns(MatchArgs.Any, returnThis, returnThese);
|
||||
}
|
||||
|
||||
private static void Returns<T>(MatchArgs matchArgs, T returnThis, params T[] returnThese)
|
||||
private static ConfiguredCall Returns<T>(MatchArgs matchArgs, T returnThis, params T[] returnThese)
|
||||
{
|
||||
var context = SubstitutionContext.Current;
|
||||
IReturn returnValue;
|
||||
|
@ -40,10 +40,10 @@ namespace NSubstitute
|
|||
{
|
||||
returnValue = new ReturnMultipleValues<T>(new[] {returnThis}.Concat(returnThese));
|
||||
}
|
||||
context.LastCallShouldReturn(returnValue, matchArgs);
|
||||
return context.LastCallShouldReturn(returnValue, matchArgs);
|
||||
}
|
||||
|
||||
private static void Returns<T>(MatchArgs matchArgs, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)
|
||||
private static ConfiguredCall Returns<T>(MatchArgs matchArgs, Func<CallInfo, T> returnThis, params Func<CallInfo, T>[] returnThese)
|
||||
{
|
||||
var context = SubstitutionContext.Current;
|
||||
IReturn returnValue;
|
||||
|
@ -56,7 +56,7 @@ namespace NSubstitute
|
|||
returnValue = new ReturnMultipleFuncsValues<T>(new[] { returnThis }.Concat(returnThese));
|
||||
}
|
||||
|
||||
context.LastCallShouldReturn(returnValue, matchArgs);
|
||||
return context.LastCallShouldReturn(returnValue, matchArgs);
|
||||
}
|
||||
|
||||
public static T Received<T>(this T substitute) where T : class
|
||||
|
|
Загрузка…
Ссылка в новой задаче