moq

Moq - How to mock web service call?

社会主义新天地 提交于 2019-12-07 02:16:57
问题 The using below hits an external resource that I do not want to actually hit. I want to test someResult and the code that uses it, but every time I run my unit test, this code still tries to hit the real web service. How do I use moq to fake the real call to the web service, but not mock the rest of the code within the using? public IMyInterface.SomeMethod() { // hits a web service using ( mySoapClient client = new mySoapClient() ) { var someResult = client.DoSomething(); ... ... } }

What steps to get rid of Collection was modified; enumeration operation may not execute. Error?

假装没事ソ 提交于 2019-12-07 02:15:37
问题 Our programming involves some Mock testing using In-Memory Data. Therefore, we implemented the following code that would first create In-Memory Data of Customer objects // Let us create some in-memory data // Create a list of Customer List<Customer> listOfCustomers = new List<BlahBlahExample.Domain.Objects.Customer>() { new Customer { CustomerID = "1 ",Orders = new HashSet<Order>(), CustomerDemographics = new HashSet<CustomerDemographic>(), CompanyName = "Chicago Bulls", ContactName =

How to create Mock for the abstract base class using MOQ framework?

≯℡__Kan透↙ 提交于 2019-12-07 02:10:41
问题 I want to write unit tests for MyClass but its base class is an abstract class. public class MyClass : AbstractBaseClass { } I want to Mock the AbstractBase class, so that I can skip some of the logic in its constructor when I create the MyClass instance I want to test. Is there anyway I can do this? //Unit Test var test = new Mock<IInterface>(); var derivedclass = new DerivedClass(test.Object); test.Setup(d=>d.MyMethod(It.IsAny<string>()).returns(2); derivedclass.MyMethod("hello"); //

Moq cannot create mock object of class with nullable parameter in the constructor

回眸只為那壹抹淺笑 提交于 2019-12-07 01:00:54
问题 I have this class: public class TestClass { public TestClass(int? foo, string bar) { //..Something } } I am trying to mock it using MOQ like this var mockA = new Mock<A>(new object[] {(int?)1, string.Empty}) or this, var mockA = new Mock<A>(new object[] {1 as int?, string.Empty}) even this var mockA = new Mock<A>(new object[] {(int?)null, string.Empty}) When I try to get the object like this var objA = mockA.Object it throws this exception Can not instantiate proxy of class: TestClass. Could

Mocking indexed property

断了今生、忘了曾经 提交于 2019-12-06 22:05:46
问题 I am writing unit tests using Moq. I have created a mock object. Now when i try to mock its property i am getting error "An expression tree may not contain an indexed property" here is my code. public Node GetNode(IMyInterface interface, string itemName) { return interface.Items[itemName]; } Here is the unit test var expected = new Node(); var itemName = "TestName"; var mock = new Mock<IMyInterface>(); mock.Setup(f => f.Items[itemName]).Returns(expected); var target = new MyClass(); var

How can I create a SerialPort mock using the Moq library?

余生颓废 提交于 2019-12-06 20:53:48
问题 I have to write a lot of code that deals with serial ports. Usually there will be a device connected at the other end of the wire and I usually create my own mocks to simulate their behavior. I'm starting to look at Moq to help with my unit tests. It's pretty simple to use it when you need just a stub, but I want to know if it is possible and if yes how do I create a mock for a hardware device that responds differently according to what I want to test. A simple example: One of the devices I

Using Moq to Stub an interface method [duplicate]

会有一股神秘感。 提交于 2019-12-06 20:51:01
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: How to mock a method that returns an int with MOQ Here's my interface: public interface ICalenderService { DateTime Adjust(DateTime dateToAdjust, BusinessDayConvention convention, List<HolidayCity> holidayCities); } I've done some research and it seems you can mock this real easily, but I want to stub this out using Moq so that I can pass the stub into my other class constuctors and have the stub return

TypeMock VS JustMock (VS RhinoMock,Moq…): current situation in 2011? [closed]

你离开我真会死。 提交于 2019-12-06 20:18:41
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 29 days ago . I've started TDD some weeks ago. I have to do Unit Tests on a C# code full of non-virtual methods and there is no much interface either. Therefore, after I've been studying RhinoMock and Moq, a proxy solution wasn't enough: what I need is an isolation thanks to a profiler.

How to write unit test for private method in c# using moq framework?

二次信任 提交于 2019-12-06 17:36:33
问题 I want to write unit test for private method in C# using moq framework, I've search in StackOverFlow and Google, but I cannot find the expected result. Please help me if you can. 回答1: You can't, at least not with Moq. But more importantly, you shouldn't . First off, you don't test methods , you test behaviours . Second, in order to test behaviours, you exercise a type's public API and verify the outcomes of that exercise. Private methods are implementation details. You don't want to verify

How can I decouple my application from my membership service?

最后都变了- 提交于 2019-12-06 15:43:34
I'm working on an ASP.NET MVC 4 project that uses Castle Windsor. One of the controllers has a dependency on MembershipService: public class FooController : Controller { private readonly MembershipService MembershipService; public FooController( MembershipService membershipService ) { MembershipService = membershipService; } [Authorize( Roles = "Administrator" )] public ActionResult DoSomething() { var user = MembershipService.GetUser( User.Identity.Name ); // etc... } } When I started writing unit tests for this controller (using Moq), I ran into a problem: private Mock<MembershipService>