-
-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for success
condition parameter in When
.
#1237
Comments
That Why don't you simply put your logic in a |
@stakx so why is Moq using that internal implementation instead on I'm try to make my custom sequencer mock.InMyCustomSequencer(....)
.Setup(x => x.Method())
.Returns(10)
.Callback(() => developerChoosenCallback()) Basically I'm augmenting Moq features available to a developer to cover a particular use case. |
Actually now I see, |
Not sure what your "sequencers" are exactly, but if they're in any way related or similar to That being said, Moq's sequences are very limited, and conditional setups that they're based on have a whole bunch of warts. Instead of adding another wart to conditional setups (exposing the internal |
My goal is to test a service call that internally uses an unit of work multiple times to call repositories. void SomeServiceMethod()
{
uow.BeginTran();
repo1.DoThings();
repo2.DoOtherThings();
uow.Commit();
// do other things
uow.BeginTran();
repo3.DoSomething();
repo4.DoEverything();
uow.Commit();
} Of course I we need to mock UnitOfWork and all of the repositories to make a good unit test. I want to verify that repositories are being called in the same transaction in the correct order. mockRepo1.InTransaction(mockUnitOfWork).Setup(x => x.DoThings(), transactionIndex: 0).Verifiable();
mockRepo2.InTransaction(mockUnitOfWork).Setup(x => x.DoOtherThings(), transactionIndex: 0).Verifiable();
mockRepo3.InTransaction(mockUnitOfWork).Setup(x => x.DoSomething(), transactionIndex: 1).Verifiable();
mockRepo4.InTransaction(mockUnitOfWork).Setup(x => x.DoEverything(), transactionIndex: 1).Verifiable(); At the moment I'm using reflection to make use of that hidden parameter: I know this is a horrible practice. /// <summary>
/// Internal <see cref="Moq.Language.Flow.WhenPhrase{T}"/> class used by sequence
/// </summary>
private readonly static Type _whenPhraseGenericType = typeof(Mock).Assembly.GetTypes().First(t => t.FullName == "Moq.Language.Flow.WhenPhrase`1");
/// <summary>
/// Internal <see cref="Condition"/> class used by sequence
/// </summary>
private readonly static ConstructorInfo _conditionCtor = typeof(Mock).Assembly.GetTypes().First(t => t.FullName == "Moq.Condition").GetConstructor(new[] { typeof(Func<bool>), typeof(Action) })!;
internal ISetupConditionResult<TMock> For<TMock>(Mock<TMock> mock, int transactionIndex, bool verifySequence) where TMock : class
{
while (this._sequenceLengths.Count <= transactionIndex)
{
this._sequenceLengths.Add(0);
}
int expectationPosition = this._sequenceLengths[transactionIndex]++;
//var condition = new Condition(() => this._transactionIndex == transactionIndex && (expectationPosition == this._sequenceStep || !verifySequence), NextStep);
object condition = _conditionCtor.Invoke(new object[] { () => this._transactionIndex - 1 == transactionIndex && (expectationPosition == this._sequenceStep || !verifySequence), NextStep });
//return new WhenPhrase<TMock>(mock, condition);
return (ISetupConditionResult<TMock>)_whenPhraseGenericType.MakeGenericType(typeof(TMock)).GetConstructor(new[] { mock.GetType(), condition.GetType() })!.Invoke(new object[] { mock, condition });
} I get what you're saying though. And I agree with you, there's should be a better way of customizing sequence logics. I'm going to close this issue, and if I come up with an idea, I'll report it in the issue that you linked. |
The current
MockSequence
implementation looks like this:Do do a custom similar thing we'd use the
When
method:But it doesn't expose the
success
action.What I'm suggesting is to evolve this signature to:
I'd like to use this to setup a custom condition that depends on that success.
Thanks
The text was updated successfully, but these errors were encountered: