Skip to content

Remove necessity of Moq on external methods #110

@HuwLittle

Description

@HuwLittle

Hi guys,

I really like the idea of this package, but having a hard dependency on Moq is not something that's palatable for my team.

I'm happy with the internal workings of this package using Moq, but would like this package to enable the end-user to use whatever mocking framework they want when using this package.

It seems like this is partially supported already with the Silo ServiceProvider (allowing an instance or a Mock -

public Mock<T> AddServiceProbe<T>() where T : class
{
var mock = new Mock<T>();
_services.Add(typeof(T), mock.Object);
return mock;
}
public T AddService<T>(T instance)
{
_services.Add(typeof(T), instance);
return instance;
}

But for grain probes, it seems like I can't register an instance of the grain via the AddProbe method on the Silo without using Moq in some way -

public static void AddProbe<T>(this TestKitSilo silo, Func<IGrainIdentity, IMock<T>> factory)
where T : class, IGrain
{
if (silo == null)
{
throw new ArgumentNullException(nameof(silo));
}
silo.GrainFactory.AddProbe(factory);
}

I'm thinking of overloading the GrainProbeExtensions' AddProbe method to allow the user to pass in simply a factory method that returns the interface of the grain rather than a IMock, as it seems like we're only using the instance in the GrainFactory regardless.

private T GetProbe<T>(IGrainIdentity identity, string grainClassNamePrefix)
where T : IGrain
{
var key = GetKey(identity, typeof(T), grainClassNamePrefix);
if (_probes.TryGetValue(key, out var grain))
{
return (T)grain;
}
//If using strict grain probes, throw the exception
if (_options.StrictGrainProbes)
{
throw new Exception($"Probe {identity.IdentityString} does not exist for type {typeof(T).Name}. " +
"Ensure that it is added before the grain is tested.");
}
else
{
IMock<IGrain> mock;
if (_probeFactories.TryGetValue(typeof(T), out var factory))
{
mock = factory(identity);
}
else
{
mock = Activator.CreateInstance(typeof(Mock<>).MakeGenericType(typeof(T))) as IMock<IGrain>;
}
//Save the newly created grain for the next call
grain = mock?.Object;
_probes.Add(key, grain);
}
return (T)grain;
}

Please let me know what you think and if I'm missing something that would make this undoable.

Thanks,

Huw

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions