moq

Using Moq to test methods that accept non-primative arguments

心不动则不痛 提交于 2020-01-05 08:28:12
问题 I'm trying to write a test for an ASP.Net MVC controller action. I'd like to test that the action invokes a particular method on an injected service, so I'm mocking the service and using .Verify. So in the simple case, I have the following action in my controller: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(string title) { _cmsService.AddPage(title); return View("Edit"); } using the service interface... public interface ICmsService { void AddPage(string request); } and the

System.IO.Abstraction can't find IStreamWriter

社会主义新天地 提交于 2020-01-05 05:26:05
问题 I am trying to unit test a method that calls StreamWriter, I am trying to use System.IO.Abstraction in order to mock StreamWriter however i can't find the interface on the last Nuget looked into the source code as well but have no idea what is the replacement for this, other stuff like FileInfo is working as expected. Thanks, 回答1: I was also looking for how to mock a FileStream via System.IO.Abstractions and couldn't see it initially. It's Hanging off the FileInfo Object. It results in

Moq Does Not Preserve Property Values

故事扮演 提交于 2020-01-05 05:08:08
问题 I have a mocked object in my unit tests that has a property. At a certain point in my test, the code blows up because that property returns null, even though it was just set to a non-null value. I have tried using SetupAllProperties() , SetupProperty() with just that property, and explicitly building SetupGet and SetupSet setAlignment = new Alignment(); mockSetAlignmentRandomizer.SetupAllProperties(); mockSetAlignmentRandomizer.SetupSet(r => r.SetAlignment = It.IsAny<Alignment>()).Callback

Mocking Mapper.Map() in Unit Testing

二次信任 提交于 2020-01-05 04:51:10
问题 I have following line of code in my controller and need to Setup this for Unit Test. var result = data.ToList().Select(x=> this.mapper.Map<A_Class, B_Class> (x)).ToList(); I am doign something like following this.mapperMock.Setup(x => x.Map<A_Class, B_Class>(AAA)).Returns(expectedResult); Can anyone suggest what should be AAA and what should be expectedResult? In my controller my linq works foreach object of A_Class in Data. How can this be setup in UnitTest 回答1: If you want to return your

validate MOQ unit test method return value

天涯浪子 提交于 2020-01-05 03:35:07
问题 I have the below class and test class written using Moq: public class Mytest : testin { public int getId(int id) { int s = 2; return s; } } test class: private Mock<testin> _mock; [TestInitialize] public void Setup() { _mock = new Mock<testin>(); } [TestMethod] public void returngetId() { // Build up our mock object _mock.Setup(x => x.getId(It.IsAny<int>())).Returns(1) } I'm returning 2 from the function and in unit test cases checking for the value 1 . As per my understanding the test cases

Mocking Entity Framework 6 ObjectResult with Moq

不打扰是莪最后的温柔 提交于 2020-01-04 18:14:40
问题 How can I mock the Entity Framework 6 ObjectResult with Moq so that I can unit test my code that relies on an EF database connection? Having read numerous questions and answers along these lines, and gleaned many nuggets from what I've read, I've implemented what I feel is a reasonably elegant solution and felt that I should share it, since the community here helped me get there. Thus, I'll proceed to answer this question, and potentially open myself up to some mockery (pun intended): 回答1:

Mocking Entity Framework 6 ObjectResult with Moq

白昼怎懂夜的黑 提交于 2020-01-04 18:13:27
问题 How can I mock the Entity Framework 6 ObjectResult with Moq so that I can unit test my code that relies on an EF database connection? Having read numerous questions and answers along these lines, and gleaned many nuggets from what I've read, I've implemented what I feel is a reasonably elegant solution and felt that I should share it, since the community here helped me get there. Thus, I'll proceed to answer this question, and potentially open myself up to some mockery (pun intended): 回答1:

Moq Setup InvalidCastException when Mocking an interface that implements multiple interfaces having the same method signature

蓝咒 提交于 2020-01-04 13:45:07
问题 So I have the following code: interface Parent1 { void Foo(); } interface Parent2 { void Foo(); } interface ChildInterface : Parent1, Parent2 { } I want to mock ChildInterface and setup its Foo(). So I used Moq to do this: var c = new Mock<ChildInterface>(MockBehavior.Strict); c.Setup(p1 => ((Parent1)p1).Foo()); c.Setup(p2 => ((Parent2)p2).Foo()); It cannot just accept without doing an explicit casting. From explanations from this SO question. So I did that. And it compiles without errors !

MOQ Mock void method that changes a field value

最后都变了- 提交于 2020-01-04 09:05:15
问题 I am new to MOQ and Mocking. Assume I have a class like this, with a void method that changes a value: public class Sample { public virtual int Number { get; set; } public virtual void Check(int a) { this.Number = a > 10 ? this.Number = 100 : this.Number = 200; } } I want to test the void method to make sure it is changing the local field Number . [Fact] public void TestSampleClass() { var sut = new Mock<Sample>(); sut.Setup(s => s.Number).Returns(50); sut.Setup(s => s.Check(It.IsAny<int>()))

All unit tests throwing BadImageFormatException with Moq?

≯℡__Kan透↙ 提交于 2020-01-04 06:53:28
问题 I'm currently in the process of increasing code coverage on our software products and have ran into an issue; all of my unit tests (when compiled using 'Any CPU') are failing due to throwing a 'BadImageFormatException'. This exception can be circumvented by building the solution using 'x86' instead of 'Any CPU', however the requirements are such that we need to be able to run them using Any CPU/x64 bit. All unit tests involving Moq follow pretty much the same format: [TestMethod] public void