-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
Mock.Of() and MockRepository.VerifyAll() #617
Comments
Do you know the
|
I am familiar with |
It doesn't appear to make much sense to give a repository the ability to add any (externally created) mock to it. IMHO it would be much preferable to just do the obvious and add a method that corresponds to Since the public API of Moq 4 is currently frozen due to Moq 5 being made ready for initial publication, we can't push such an API enhancement forward right now (even though it would be a trivial addition). I shouldn't even suggest the following, as it intentionally breaks an abstraction... but you could do this: sealed class MockOfRepository : MockRepository
{
public MockOfRepository(MockBehavior defaultBehavior) : base(defaultBehavior) { }
public T Of<T>(Expression<Func<T, bool>> predicate) where T : class
{
var mockObject = Mock.Of<T>(predicate);
var mock = Mock.Get(mockObject);
var mocks = (ICollection<Mock>)this.Mocks;
mocks.Add(mock);
return mockObject;
}
} Obviously, assuming that |
Would you accept a PR to put Agreed, this solution is perfect except for the obvious fragility of assuming unsafe internals that are already flagged to change. |
/cc @kzu:
|
@robrich - It appears that I've been away too long from Moq's code base, or from this issue... but I'm not exactly sure anymore why we've even been discussing this. Aren't the following existing methods what you're looking for?: The only catch here is that these existing methods return an MockRepository mocks = ...;
var foo = mocks.Of<IFoo>(f => ...).First(); |
@robrich - Can we close this issue, or is the above solution |
Yes, we can close this. All else being equal, I still see value in making the |
Problem: Mocks created with
Mock.Of()
aren't inMockRepository
.Propose:
MockRepository.Add(Mock<T> mock)
used like so:The text was updated successfully, but these errors were encountered: