moq

Mocking EventHandler

时光总嘲笑我的痴心妄想 提交于 2019-12-23 11:52:57
问题 Having defined an interface public interface IHandlerViewModel { EventHandler ClearInputText { get; } } I would like to test if ClearInputText is invoked by some method. To do so I do something like this SomeType obj=new SomeType(); bool clearCalled = false; var mockHandlerViewModel=new Mock<IHandlerViewModel>(); mockHandlerViewModel.Setup(x => x.ClearInputText).Returns(delegate { clearCalled = true; }); obj.Call(mockHandlerViewModel.Object);//void Call(IHandlerViewModel); Assert.IsTrue

Mocking EventHandler

泄露秘密 提交于 2019-12-23 11:52:10
问题 Having defined an interface public interface IHandlerViewModel { EventHandler ClearInputText { get; } } I would like to test if ClearInputText is invoked by some method. To do so I do something like this SomeType obj=new SomeType(); bool clearCalled = false; var mockHandlerViewModel=new Mock<IHandlerViewModel>(); mockHandlerViewModel.Setup(x => x.ClearInputText).Returns(delegate { clearCalled = true; }); obj.Call(mockHandlerViewModel.Object);//void Call(IHandlerViewModel); Assert.IsTrue

Define method implementation in mock object using Moq

假装没事ソ 提交于 2019-12-23 09:46:34
问题 This is the situation. I had async call so I needed to make Mid tier for this in order to be able to test it. request.BeginGetResponse(new AsyncCallback(LoginCallback), requestState); So, in order to be able to test this without real request, I created interface which I can mock. public interface IRequestSender { void Send(HttpWebRequest request, AsyncCallback internalCallback, object requestState); } Then in implementation I can use call like that one above and I can provide some mock class

Mock a constructor with MOQ

浪尽此生 提交于 2019-12-23 08:03:06
问题 I have a class B with a constructor parameter of Type class A. I want that class A is mocked when I create a mock for class B. How can I do this? I tried MockBehavior Loose/Strict but this did not help! 回答1: If you are mocking classes you can pass in the constructor arguments when calling new Mock<T> : So if you have the classes: public class A {} public class B { private readonly A a; public B(A a) { this.a = a; } } The following code creates a mock B with a mock A: var mockA = new Mock<A>()

Moq First() Last() and GetEnumerator() wierdness

做~自己de王妃 提交于 2019-12-23 07:30:10
问题 I am Moqing my Route Parts from a rps = new List <IRoutePart> ... (3 Route Parts) and Moqing GetEnumerator() for my Route as below route.Setup(ro => ro.GetEnumerator()).Returns(rps.GetEnumerator()); but the Moq fails in the following code with "Sequence contains no elements" on the call to Last() o.Route.Any(rp => rp.IsNonTowLocation && rp != o.Route.First() && rp != o.Route.Last()) Looking at First() Last() in the immediate windows I find the values change if I execute First() Last()

Mock a Method with Moq and make moq.Object available there

纵然是瞬间 提交于 2019-12-23 06:23:12
问题 The code structure and classes emerge from this topic: C# Mock a Class With an Internal Property Setter Here a summary of what I have. I have the following Interface : public interface IPosition { // Some properties here which must be accessed in CalcValueChangeAtDate method. int Size { get; } ... double CalcValueChangeAtDate(DateTime fromDate, DateTime toDate); } And I implemented the interface in the class RealPosition like this: public class RealPosition : IPosition { /* IPosition

Help with this unit test using Moq

你说的曾经没有我的故事 提交于 2019-12-23 03:16:25
问题 I have the following so far which I am trying to unit test: private Mock<IDBFactory> _mockDbFactory; private IArticleManager _articleManager; [Setup] public Setup() { _mockDbFactory = new Mock<IDBFactory>(); _articleManager = new ArticleManager(_mockDbFactory); } [Test] public void load_article_by_title() { string title = "sometitle"; // _dbFactory.GetArticleDao().GetByTitle(title); <!-- need to mock this _mockDBFactory.Setup(x => x.GetArticleDao().GetByTitle(It.IsAny<string>()));

Help with this unit test using Moq

旧时模样 提交于 2019-12-23 03:16:01
问题 I have the following so far which I am trying to unit test: private Mock<IDBFactory> _mockDbFactory; private IArticleManager _articleManager; [Setup] public Setup() { _mockDbFactory = new Mock<IDBFactory>(); _articleManager = new ArticleManager(_mockDbFactory); } [Test] public void load_article_by_title() { string title = "sometitle"; // _dbFactory.GetArticleDao().GetByTitle(title); <!-- need to mock this _mockDBFactory.Setup(x => x.GetArticleDao().GetByTitle(It.IsAny<string>()));

AutoFixture mixing PropertyData with multiple entries and AutoData (using AutoMoqCustomization)

一个人想着一个人 提交于 2019-12-23 03:15:09
问题 I've looked at both of these similar SO questions: AutoFixture: PropertyData and heterogeneous parameters AutoFixture CompositeDataAttribute does not work with PropertyDataAttribute And they're awesome and get me nearly there. But both examples use only one entry in the emitted IEnumerable PropertyData (i.e.: yield return new object[] { 2, 4 }; -- see: https://stackoverflow.com/a/16843837/201308) This works, but it blows up whenever I want to do test over more than one object[] test data. I

AutoFixture mixing PropertyData with multiple entries and AutoData (using AutoMoqCustomization)

左心房为你撑大大i 提交于 2019-12-23 03:15:09
问题 I've looked at both of these similar SO questions: AutoFixture: PropertyData and heterogeneous parameters AutoFixture CompositeDataAttribute does not work with PropertyDataAttribute And they're awesome and get me nearly there. But both examples use only one entry in the emitted IEnumerable PropertyData (i.e.: yield return new object[] { 2, 4 }; -- see: https://stackoverflow.com/a/16843837/201308) This works, but it blows up whenever I want to do test over more than one object[] test data. I