moq

AutoMockContainer with support for automocking classes with non-interface dependencies

瘦欲@ 提交于 2019-11-30 02:25:08
I have a constructor which has a non-interface dependency: public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator) I am using the Moq.Contrib automockcontainer. If I try to automock the MainWindowViewModel class, I get an error due to the WeekNavigatorViewModel dependency. Are there any automocking containers which supports mocking of non-interface types? As Mark has shown below; yes you can! :-) I replaced the Moq.Contrib AutoMockContainer with the stuff Mark presents in his answer, the only difference is that the auto-generated mocks are

Moq and throwing a SqlException

对着背影说爱祢 提交于 2019-11-30 01:56:11
I have the following code to test that when a certain name is passed to my method, it throws a SQL exception (there is reason to that one, although it sounds a little odd). mockAccountDAL.Setup(m => m.CreateAccount(It.IsAny<string>(), "Display Name 2", It.IsAny<string>())).Throws<SqlException>(); However, this won't compile because SqlException's constructor is internal: 'System.Data.SqlClient.SqlException' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TException' in the generic type or method 'Moq.Language.IThrows.Throws()' Now, I could

Using Moq To Test An Abstract Class

大兔子大兔子 提交于 2019-11-30 01:51:45
问题 I am trying to run a unit test on a method in an abstract class. I have condensed the code below: Abstract Class: public abstract class TestAb { public void Print() { Console.WriteLine("method has been called"); } } Test: [Test] void Test() { var mock = new Mock<TestAb>(); mock.CallBase = true; var ta = mock.Object; ta.Print(); mock.Verify(m => m.Print()); } Message: Method is not public What am I doing wrong here? My goal is to test the methods inside the abstract class using he Moq

How to Mock (with Moq) Unity methods

大憨熊 提交于 2019-11-30 01:38:51
问题 Extension methods are not good for testing (that's described here: Mocking Extension Methods with Moq, http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx). But probably there are some solutions for mocking of Unity methods? In my case I have the following function: public class MyManager { public MyManager(IUnityContainer container) : base(container) { } public IResult DoJob(IData data) { IMyLog log = MyContainer.Resolve<IMyLog>(); ... use log.Id ...

Can I get Moq to add attributes to the mock class?

隐身守侯 提交于 2019-11-30 01:09:44
问题 I'm writing a command-line interface to my project. The user enters "create project foo", and it finds the controller responsible for "project" and then invokes the Create method, passing "foo" as the first argument. It relies heavily on attributes and reflection: the controller looks something like this: [ControllerFor("project")] class ProjectController { [ControllerAction("create")] public object Create(string projectName) { /* ... */ } } I'd like to use Moq in the unit tests for the

Using Moq to Mock a Func<> constructor parameter and Verify it was called twice

蓝咒 提交于 2019-11-30 00:16:33
问题 Taken the question from this article (How to moq a Func) and adapted it as the answer is not correct. public class FooBar { private Func<IFooBarProxy> __fooBarProxyFactory; public FooBar(Func<IFooBarProxy> fooBarProxyFactory) { _fooBarProxyFactory = fooBarProxyFactory; } public void Process() { _fooBarProxyFactory(); _fooBarProxyFactory(); } } I have a need to mock a Func<> that is passed as a constructor parameter, the assert that the func was call twice. When trying to mock the function var

Async methods return null

浪子不回头ぞ 提交于 2019-11-30 00:13:52
If I try to mock a type containing an async method such as : interface Foo { Task<int> Bar(); } Then the mock's Bar method is returning null. I guess Moq is choosing default(Task<int>) as default return value for my method, which is indeed null . However Moq should rather choose something like Task.FromResult(default(int)) as default value. Can I force Moq to make async methods returning non-null Tasks ? If someone is interested, I made an extension class which makes async methods stubing less verbose : public static class SetupExtensions { public static IReturnsResult<TMock> ReturnsTask<TMock

How to mock ModelState.IsValid using the Moq framework?

五迷三道 提交于 2019-11-30 00:12:49
I'm checking ModelState.IsValid in my controller action method that creates an Employee like this: [HttpPost] public virtual ActionResult Create(EmployeeForm employeeForm) { if (this.ModelState.IsValid) { IEmployee employee = this._uiFactoryInstance.Map(employeeForm); employee.Save(); } // Etc. } I want to mock it in my unit test method using Moq Framework. I tried to mock it like this: var modelState = new Mock<ModelStateDictionary>(); modelState.Setup(m => m.IsValid).Returns(true); But this throws an exception in my unit test case. Can anyone help me out here? You don't need to mock it. If

MOQ - how to mock an interface that needs to be cast to another interface?

此生再无相见时 提交于 2019-11-29 20:23:13
what I want to do is construct a moq for I1 - which is fine ... however in the course of the method that I am testing that uses this mock I need to cast it to I2 in order to access some properties that are not on I1 Interface I1 { int AProperty{get;set;}} Interface I2 {int AnotherProperty{get;set;}} I then have some objects Class O1 : I1 {} and Class O2 : O1 , I2 {} the problem is that when i have an instance of a I2 implementing object I can cast it to I1 in order to access the methods that are implmented through that interface. In code this is not a problem and everythign works as expected.

What are the capabilities of Moq and Rhino.mocks?

*爱你&永不变心* 提交于 2019-11-29 19:12:36
I cannot find a specific feature-by-feature comparison of Moq and Rhino. All the questions are "which do you like better and why", or "here's how you do a simple mock in rhino and how it's done in moq". I cannot find a deep comparison anywhere. I'm aware of the syntax differences, I'm not looking for answers about that. I am looking for a capability comparison . For example: Rhino has Expect.On() for threaded mocking. Can Moq do this? What about Multi-mocking (implementing multiple interfaces with one mock). Can Moq do this? I believe Moq can now mock Protected members. Can Rhino do this? Edit