darrencauthon / automoq Goto Github PK
View Code? Open in Web Editor NEWAuto mocking provider for Moq.
License: MIT License
Auto mocking provider for Moq.
License: MIT License
Is it possible to swap out Unity and replace it with Autofac, or any other IoC framework?
Package namespaces and probably other items changed for unity >= 5.x.
This will fail:
var mocker = new AutoMoqer()
var controller = mocker.Resolve();
mocker.GetMock().Setup(p => p.Get(id)).Returns(profile);
var p1 = profiles.Get(id);
var p2 = profiles.Get(id);
Assert.IsNotNull(p1);
//fails here, p2 is null, shouldn't be
Assert.IsNotNull(p2);
public class ContextWrapper
{
private readonly HttpContextBase _context;
public ContextWrapper(HttpContextBase context)
{
_context = context;
}
}
[Test]
public void HttpContextBaseTest()
{
var auto = new AutoMoqer();
auto.GetMock<HttpContextBase>(); // throws exception if you comment this out
var request = auto.Create<ContextWrapper>();
}
Unless you call GetMock() in the test above (which shouldn't be necessary imo), you get the following ResolutionFailedException
Resolution of the dependency failed, type = "ContextWrapper", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type HttpContextBase cannot be constructed. You must configure the container to supply this value.
On the next release of the AutoMoq, can you please include the XML documentation file AutoMoq.xml?
As a newbie to the AutoMoq tool, it would be helpful to see comments on each of the public methods.
Thanks.
Below test fails on v.1.80 and 1.71 but passes on v1.70
namespace AutoMoqIssue
{
[TestClass]
public class AutoMoqTests
{
[TestMethod]
public void can_create_parent_object_when_setInstance_is_called_on_child()
{
var autoMoq = new AutoMoqer();
var child = autoMoq.Create<Child>();
autoMoq.SetInstance<IChild>(child);
var parent = autoMoq.Create<Parent>();
Assert.IsNotNull(parent);
}
}
public interface IParent
{
}
public interface IChild
{
}
public interface IGrandChild
{
}
public class Parent : IParent
{
private readonly IChild _child;
private readonly IGrandChild _grandChild;
public Parent(IChild child, IGrandChild grandChild)
{
_child = child;
_grandChild = grandChild;
}
}
public class Child : IChild
{
private readonly IGrandChild _grandChild;
public Child(IGrandChild grandChild)
{
_grandChild = grandChild;
}
}
public class GrandChild : IGrandChild
{
}
}
in my unit-test i have to write the following code:
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentRetryEvent>()).Returns(new ComponentRetryEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentPauseEvent>()).Returns(new ComponentPauseEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentContinueEvent>()).Returns(new ComponentContinueEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentCancelEvent>()).Returns(new ComponentCancelEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentResetEvent>()).Returns(new ComponentResetEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentSkipEvent>()).Returns(new ComponentSkipEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentNextTaskEvent>()).Returns(new ComponentNextTaskEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentStateChangedEvent>()).Returns(new ComponentStateChangedEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<UpdateComponentStatusEvent>()).Returns(new UpdateComponentStatusEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<TaskConfigurationCompletedEvent>()).Returns(new TaskConfigurationCompletedEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentConfigurationCompletedEvent>()).Returns(new ComponentConfigurationCompletedEvent());
_autoMocker.GetMock<IEventAggregator>().Setup(m => m.GetEvent<ComponentExecutedEvent>()).Returns(new ComponentExecutedEvent());
I think this can be more efficient if AutoMoq can automatically setup the mocking for the methods.
when trying install it i got:
"Package AutoMoq 2.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package AutoMoq 2.0.0 supports: net45 (.NETFramework,Version=v4.5)"
I just upgraded from version 1.6.2 to version 2.0 and a lot of my unit tests are now failing because AutoMoq seems to not take into account types that are directly registered in the container.
As an example, this test is working fine with version 1.6.2 but is failing in version 2.0. Now, obj is of type IFooProxy, just like IFoo was not registered.
[Test]
public void Test_With_My_Container()
{
var container = new UnityContainer();
var mocker = new AutoMoqer(container);
// container.Resolve<IFoo>(); // If uncommenting this line, test pass
container.RegisterType<IFoo, Foo>();
var obj = container.Resolve<IFoo>();
obj.ShouldBeType<Foo>();
}
Am I missing something ?
Thanks !
When I try to mock an interface like in the example below, I get the System.InvalidOperationException: The current type, [TYPE], is an interface and cannot be constructed. Are you missing a type mapping?
This is my test:
var moqer = new AutoMoqer();
var queryString = new NameValueCollection();
queryString[CultureHelper.LangParameter] = "es";
moqer.GetMock<HttpRequestBase>()
.SetupGet(r => r.QueryString)
.Returns(queryString);
var request = moqer.Create<HttpRequestBase>();
var preferencesManager = moqer.Create<IPreferencesManager>();
CultureHelper.InitializeCulture(request, preferencesManager);
Assert.AreEqual("es", System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName);
But If I write moqer.GetMock<IPreferencesManager>() above the Creat<IPreferencesManager>() line, it passes.
The SetInstance<T>(T instance)
method of AutoMoqer
is designed to inject specific objects. Given a constructor with parameters of the same IType
the last SetInstance
call will override any previous objects. This is a major flaw in the framework.
An example
A common case is for a ViewModel
depending on two IScheduler
's the background and the dispatcher. In your ViewModelTest
you'd want to have different schedulers passed in, but this is not possible.
When compiling using VS2019, I'm getting the following warning:
Warning NU1602 AutoMoq 2.0.0 does not provide an inclusive lower bound for dependency Moq (> 4.0.0). An approximate best match of Moq 4.0.10827 was resolved.
Per request from Piotr.
Tried cherry-pick from another pull request that had this feature, but with the merge conflicts it might be easier to just re-implement it.
It would be sweet to be able to do something like:
var extraParams = new []{ new ConstructorParameter(“someValue”, 12), new ConstructorParameter(“otherValue”, “my value”) };
Mocker.Resolve(extraParams);
I have worked mostly with Ninject for DI, where you do something like the following:
kernel.Get( With.Parameters.ConstructorArgument( "i", 2 ) );
When googling around I found similar functionality in Unity as well, tho I have no first-hand experience with that;
http://stackoverflow.com/questions/787001/can-i-pass-constructor-parameters-to-unitys-resolve-method
Should Unity be upgraded?
I'm using AutoMoqTestFixture like crazy in our code. It's very convenient. However, one thing that it's missing is the ability to specify an instance for an interface instead of using auto-generated mocks. I see that the AutoMoq that underlies this class has a SetInstance method. Perhaps that could be provided as a pass-through method in AutoMoqTestFixture.
The mocker should be able to resolve a Func automatically, just as it resolves T.
Please strong name the assembly.
Is this repo dead?
The GetMock() method has a default value of MockBehavior.Default but the problem is that in this enumerable, MockBehavior.Default and MockBehavior.Loose have the exact same value.
From Moq sources:
public enum MockBehavior
{
Strict,
Loose,
Default = Loose,
}
So when the default behavior is MockBehavior.Strict and you call GetMock(MockBehavior.Loose) it's the same as calling GetMock(MockBehavior.Default) and it ends up creating a strict mock (which is the default), instead of a loose mock as requested.
The class I'm writing has its dependencies injected via constructor injection. Some of these dependencies are internal interfaces because I don't wish to expose them to users of my library. Therefore, I'd like to have an internal constructor with these dependencies as parameters and a public constructor with no parameters. I'm exposing the internals of my assembly to the DynamicProxyGenAssembly2 assembly so that internal interfaces can be mocked. Unfortunately, I've hit a snag in trying to use AutoMoqTestFixture. When the class under test is instantiated, the public constructor is used.
It would be very helpful if there was a way to have AutoMoqTestFixture invoke the internal constructor.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.