moq

The Purpose of Mocking

喜你入骨 提交于 2019-11-29 03:50:07
问题 What is the purpose of mocking? I have been following some ASP.NET MVC tutorials that use NUnit for testing and Moq for mocking. I am a little unclear about the mocking part of it though. 回答1: The purpose of mocking is to isolate the class being tested from other classes. This is helpful when a class : connects to an external resource (FileSystem, DB, network ... ) is expensive to setup, or not yet available (hardware being developed) slows down the execution of the unit tests has a non

Using Moq to override virtual methods in the same class

柔情痞子 提交于 2019-11-29 02:54:16
We are using Moq to unit test our service classes, but are stuck on how to test situations where a service method calls another service method of the same class. I tried setting the method being called to virtual, but still couldn't figure out what to do then in Moq. For example: public class RenewalService : IRenewalService { //we've already tested this public virtual DateTime? GetNextRenewalDate(Guid clientId) { DateTime? nextRenewalDate = null; //...<snip> a ton of already tested stuff... return nextRenewalDate; } //but want to test this without needing to mock all //the methods called in

Moq - How to verify that a property value is set via the setter

孤者浪人 提交于 2019-11-29 02:48:37
Consider this class: public class Content { public virtual bool IsCheckedOut {get; private set;} public virtual void CheckOut() { IsCheckedOut = true; } public virtual void CheckIn() { //Do Nothing for now as demonstrating false positive test. } } The Checkin method is intentionally empty. Now i have a few test methods to verify the status of calling each method. [TestMethod] public void CheckOutSetsCheckedOutStatusToTrue() { Content c = new Content(); c.CheckOut(); Assert.AreEqual(true, c.IsCheckedOut); //Test works as expected } [TestMethod] public void CheckInSetsCheckedOutStatusToFalse() {

Moq It.Is<> not matching

一曲冷凌霜 提交于 2019-11-29 02:20:28
This code: hub.MockedUserRepository.Setup(r => r.Update(It.IsAny<ControllUser>())) .Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null))) .Verifiable(); Will print NULL = True So i am thinking using this matching will catch it: var zombieDisconnectParameterMatcher = It.Is<ControllUser>(x => x.Zombies[0].ConnectionId == null); hub.MockedUserRepository.Setup(r => r.Update(zombieDisconnectParameterMatcher)) .Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null))) .Verifiable(); But it does not. Why? By

“Short circuiting” void methods with Moq?

淺唱寂寞╮ 提交于 2019-11-29 01:38:56
问题 my team has made the decision recently to use Moq as our mocking framework for its tremendous flexibility and highly readable syntax. As we're new to it, I'm stumbling on what appears to be simple questions--searches (here, Google, etc.) find plenty of discussions on other nuances of Moq, but not necessarily what I'm after, and the few seemingly related questions have turned into red herrings. We're testing a class that has an external dependency (Amazon SimpleDb to be precise) but don't want

How to moq a NetworkStream in a unit test?

倖福魔咒の 提交于 2019-11-29 01:27:29
I'm using Moq & NUnit as a unit test framework. I've written a method that is given a NetworkStream object as a parameter: public static void ReadDataIntoBuffer(NetworkStream networkStream, Queue dataBuffer) { if ((networkStream != null) && (dataBuffer != null)) { while (networkStream.DataAvailable) { byte[] tempBuffer = new byte[512]; // read the data from the network stream into the temporary buffer Int32 numberOfBytesRead = networkStream.Read(tempBuffer, 0, 512); // move all data into the main buffer for (Int32 i = 0; i < numberOfBytesRead; i++) { dataBuffer.Enqueue(tempBuffer[i]); } } }

Moq, SetupGet, Mocking a property

断了今生、忘了曾经 提交于 2019-11-29 01:20:02
问题 I'm trying to mock a class, called UserInputEntity , which contains a property called ColumnNames : (it does contain other properties, I've just simplified it for the question) namespace CsvImporter.Entity { public interface IUserInputEntity { List<String> ColumnNames { get; set; } } public class UserInputEntity : IUserInputEntity { public UserInputEntity(List<String> columnNameInputs) { ColumnNames = columnNameInputs; } public List<String> ColumnNames { get; set; } } } I have a presenter

How to Moq Entity Framework SqlQuery calls

筅森魡賤 提交于 2019-11-29 01:19:52
I've been able to mock DbSet 's from entity framework with Moq using this link . However, I would now like to know how I could mock the call to SqlQuery. Not sure if this is possible or how as it relies on the mocked db context knowing what "query" is being called. Below is what I am trying to mock. var myObjects = DbContext.Database .SqlQuery<MyObject>("exec [dbo].[my_sproc] {0}", "some_value") .ToList(); I currently haven't tried anything as did not know how to start mocking this example. The mocking of the DbSet is below and to re-iterate, I can correctly mock returning a DbSet of MyObject

AutoMockContainer with support for automocking classes with non-interface dependencies

别说谁变了你拦得住时间么 提交于 2019-11-28 23:18:53
问题 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

MOQ - LINQ Predicates in Setup Method

耗尽温柔 提交于 2019-11-28 22:33:29
问题 In my method, I have my repository doing this: bool isConditionMet = MyRepository.Any(x => x.Condition == true); I am attempting to mock this using MOQ like so: MyMockedRepository.Setup(x => x.Any(y => y.Condition == true)).Returns(true); However, when the code executes, the repository call always returns false. Is there a way to do this using MOQ? ** EDIT - Adding code per request ** I am using NHibernate so my Any method is in my base repository and implemented as such: public virtual bool