moq

What is AutoFixture AutoMoq?

穿精又带淫゛_ 提交于 2019-12-02 20:04:15
I was looking at nuget and wanted to import moq when I noticed AutoFixture AutoMoq. I see that AutoFixture is to help write TDD faster but I can't find any examples of AutoMoq and how it is different then AutoFixture. Can someone point me to this AutoMoq so I can see what it is doing. Mark Seemann In short, AutoFixture.AutoMoq is an extension that turns AutoFixture into an Auto-Mocking Container using the Moq dynamic mock library. There's also a similar extension for AutoFixture that enables auto-mocking with Rhino Mocks . This article introduces auto-mocking for AutoFixture: http://blog.ploeh

Mock an update method returning a void with Moq

谁说胖子不能爱 提交于 2019-12-02 20:00:37
In my test, I defined as data a List<IUser> with some record in. I'd like setup a moq the methode Update , this method receive the user id and the string to update. Then I get the the IUser and update the property LastName I tried this : namespace Tests.UnitTests { [TestClass] public class UsersTest { public IUsers MockUsersRepo; readonly Mock<IUsers> _mockUserRepo = new Mock<IUsers>(); private List<IUser> _users = new List<IUser>(); [TestInitialize()] public void MyTestInitialize() { _users = new List<IUser> { new User { Id = 1, Firsname = "A", Lastname = "AA", IsValid = true }, new User { Id

How to UnitTest a Function in a mocked method

戏子无情 提交于 2019-12-02 18:24:30
问题 How can I test the DeleteAppointmentById here? Func<IDataAdapterRW, IEnumerable<uint>> function = db => DeleteAppointmentById(db, appointmentId); return _dataContextProvider.GetContextRW().Run(function); _dataContextProvider is mocked with moq. If I run the test it never enters DeleteAppointmentById of course The method to test: public IEnumerable<uint> DeleteAppointment(uint appointmentId) { Func<IDataAdapterRW, IEnumerable<uint>> function = db => DeleteAppointmentById(db, appointmentId);

Why use It.is<> or It.IsAny<> if I could just define a variable?

岁酱吖の 提交于 2019-12-02 17:58:16
Hi I've been using moq for a while when I see this code. I have to setup a return in one of my repo. mockIRole.Setup(r => r.GetSomething(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(ReturnSomething); I have three parameters and I just saw these in one of articles or blog on the net. What is the use of It.Is<> or It.IsAny<> for an object? if I could use Guid.NewGuid() or other types then why use It.Is ? I'm sorry I'm not sure if my question is right or am I missing some knowledge in testing. But it seems like there is nothing wrong either way. Patrick Quirk Using It.IsAny<> ,

Mock objects - Setup method - Test Driven Development

时光总嘲笑我的痴心妄想 提交于 2019-12-02 17:28:27
I am learning Test Driven Development and trying to use Moq library for mocking. What is the purpose of Setup method of Mock class? Igor Zevaka The default behaviour of a Moq Mock object is to stub all methods and properties. This means that a call to that method/property with any parameters will not fail and will return a default value for the particular return type. You call Setup method for any or all of the following reasons: You want to restrict the input values to the method. public interface ICalculator { int Sum(int val1, val2); } var mock = new Mock<ICalculator>(); mock.Setup(m=>m.Sum

How to mock an SqlDataReader using Moq - Update

我是研究僧i 提交于 2019-12-02 17:17:27
I'm new to moq and setting up mocks so i could do with a little help. How do I mock up an SqlDataReader using Moq? Update After further testing this is what I have so far: private IDataReader MockIDataReader() { var moq = new Mock<IDataReader>(); moq.Setup( x => x.Read() ).Returns( true ); moq.Setup( x => x.Read() ).Returns( false ); moq.SetupGet<object>( x => x["Char"] ).Returns( 'C' ); return moq.Object; } private class TestData { public char ValidChar { get; set; } } private TestData GetTestData() { var testData = new TestData(); using ( var reader = MockIDataReader() ) { while ( reader

Rhino mock vs Typemock vs JustMock vs [closed]

瘦欲@ 提交于 2019-12-02 16:39:50
I need to choose mock framework to new project. What are the pros and cons for those frameworks? Any comparison table? I know that JustMock is i beta stage but it's look very good right now (very similar to TypeMock) Edit: I'v What about MS Mole? Dror Helper Before there was JustMock this question was asked and the answers can be found here . There is a very good Mocking framework comparison - it doesn't have JustMock yet but you get to see the syntax and capabilities of each .NET mocking framework. RhinoMocks (and Moq ) are both open source free to use projects that can create fake objects by

Verify a method call using Moq

橙三吉。 提交于 2019-12-02 15:41:06
I am fairly new to unit testing in C# and learning to use Moq. Below is the class that I am trying to test. class MyClass { SomeClass someClass; public MyClass(SomeClass someClass) { this.someClass = someClass; } public void MyMethod(string method) { method = "test" someClass.DoSomething(method); } } class Someclass { public DoSomething(string method) { // do something... } } Below is my TestClass: class MyClassTest { [TestMethod()] public void MyMethodTest() { string action="test"; Mock<SomeClass> mockSomeClass = new Mock<SomeClass>(); mockSomeClass.SetUp(a => a.DoSomething(action)); MyClass

MVC 3: How to learn how to test with NUnit, Ninject, and Moq?

£可爱£侵袭症+ 提交于 2019-12-02 13:57:47
Short version of my questions: Can anyone point me toward some good, detailed sources from which I can learn how to implement testing in my MVC 3 application, using NUnit, Ninject 2, and Moq? Can anyone here help clarify for me how Controller-Repository decoupling, mocking, and dependency injection work together? Longer version of my questions: What I'm trying to do ... I am currently beginning to create an MVC 3 application, which will use Entity Framework 4, with a database first approach. I want to do this right, so I am trying to design the classes, layers, etc., to be highly testable. But

Different return values the first and second time with Moq

℡╲_俬逩灬. 提交于 2019-12-02 13:55:54
I have a test like this: [TestCase("~/page/myaction")] public void Page_With_Custom_Action(string path) { // Arrange var pathData = new Mock<IPathData>(); var pageModel = new Mock<IPageModel>(); var repository = new Mock<IPageRepository>(); var mapper = new Mock<IControllerMapper>(); var container = new Mock<IContainer>(); container.Setup(x => x.GetInstance<IPageRepository>()).Returns(repository.Object); repository.Setup(x => x.GetPageByUrl<IPageModel>(path)).Returns(() => pageModel.Object); pathData.Setup(x => x.Action).Returns("myaction"); pathData.Setup(x => x.Controller).Returns("page");