Describe the solution you'd like
At some point, Rocks had RockRepository. Then I removed it. I can't really remember why. But I think it would be nice to have it again.
Would be defined in Rocks, something like this:
public sealed class RockRepository
: IDisposable
{
private bool isDisposed;
private readonly List<Expectations> createdExpectations = [];
public T Create<T>()
where T : Expectations
where T : new()
{
ObjectDisposedException.ThrowIf(this.isDisposed);
var expectations = new T();
this.createdExpectations.Add(expectations);
return expectations;
}
public void Dispose()
{
ObjectDisposedException.ThrowIf(this.isDisposed);
foreach (var createdExpectations in this.createdExpectations)
{
createdExpectations.Verify();
}
this.isDisposed = true;
}
}
Then, it could be used like this:
[assembly: Rock(typeof(IStuff), BuildType.Create)]
using var repository = new RockRepository();
var stuffExpectations = repository.Create<IStuffCreateExpectations>();
stuffExpectations.Methods.Do();
var mock = stuffExpectations.Instance();
mock.Do();
public interface IStuff
{
void Do();
}
Note that no Verify() call is needed. If multiple mocks are in play in a test, using a repository to group them together would be pretty clean.
A problem is that the expectations type, and its no-argument constructor, are currently both internal, so RockRepository wouldn't see the constructor. I could either change that so they're public (or at least the constructor is), or, change Create<T>() to Add<T>():
public T Add<T>(T expectations)
where T : Expectations
{
ObjectDisposedException.ThrowIf(this.isDisposed);
this.createdExpectations.Add(expectations);
return expectations;
}
Then a new repository would be created like this:
using var repository = new RockRepository();
var stuffExpectations = repository.Add<new IStuffCreateExpectations());
Describe alternatives you've considered
Don't add the repository.
Describe the solution you'd like
At some point, Rocks had
RockRepository. Then I removed it. I can't really remember why. But I think it would be nice to have it again.Would be defined in Rocks, something like this:
Then, it could be used like this:
Note that no
Verify()call is needed. If multiple mocks are in play in a test, using a repository to group them together would be pretty clean.A problem is that the expectations type, and its no-argument constructor, are currently both
internal, soRockRepositorywouldn't see the constructor. I could either change that so they're public (or at least the constructor is), or, changeCreate<T>()toAdd<T>():Then a new repository would be created like this:
Describe alternatives you've considered
Don't add the repository.