moq

Setup Method With Params Array

烈酒焚心 提交于 2019-11-27 06:34:07
问题 I am developing tests for an application. There's a method that has a params array as a parameter. I have set up the method using Moq but when I run the test, the return value of the method is null, which means it is not being mocked. Here's a code sample: public interface ITicketManager { string GetFirstTicketInQueueIfMatches(params string[] ticketsToMatch); } public class TicketManager : ITicketManager { private Queue<string> ticketQueue = new Queue<string>(); public string

Mocking IDocumentQuery in Unit Test that uses Linq queries

别说谁变了你拦得住时间么 提交于 2019-11-27 06:10:43
问题 I am writing unit tests for DocumentDBRepository but I got a null reference exception. I use Moq framework and XUnit. Here's my methods in DocumentDBRepository class. public class DocumentDBRepository<T> : IRepository<T> where T: class { private static string DatabaseId; private static string CollectionId; private static IDocumentClient client; public DocumentDBRepository(IDocumentClient documentClient, string databaseId, string collectionId) { DatabaseId = databaseId; CollectionId =

How do I use Moq and DbFunctions in unit tests to prevent a NotSupportedException?

江枫思渺然 提交于 2019-11-27 05:51:11
问题 I'm currently attempting to run some unit tests on a query that is running through the Entity Framework. The query itself runs without any issues on the live version, but the unit tests are always failing. I've narrowed this down to my usage of DbFunctions.TruncateTime, but I don't know of a way around this to get the unit tests to reflect what is happening on the live server. Here is the method that I am using: public System.Data.DataTable GetLinkedUsers(int parentUserId) { var today =

Mocking The RouteData Class in System.Web.Routing for MVC applications

随声附和 提交于 2019-11-27 05:39:02
问题 I'm trying to test some application logic that is dependent on the Values property in ControllerContext.RouteData. So far I have // Arrange var httpContextMock = new Mock<HttpContextBase>(MockBehavior.Loose); var controllerMock = new Mock<ControllerBase>(MockBehavior.Loose); var routeDataMock = new Mock<RouteData>(); var wantedRouteValues = new Dictionary<string, string>(); wantedRouteValues.Add("key1", "value1"); var routeValues = new RouteValueDictionary(wantedRouteValues); routeDataMock

Moq + Unit Testing - System.Reflection.TargetParameterCountException: Parameter count mismatch

三世轮回 提交于 2019-11-27 05:13:47
I'm tring to use a lambda with a multiple-params function but Moq throws this exception at runtime when I attempt to call the mock.Object.Convert(value, null, null, null); line. System.Reflection.TargetParameterCountException: Parameter count mismatch The code is: var mock = new Mock<IValueConverter>(); mock.Setup(conv => conv.Convert(It.IsAny<Object>(), It.IsAny<Type>(), It.IsAny<Object>(), It.IsAny<CultureInfo>())).Returns((Int32 num) => num + 5); var value = 5; var expected = 10; var actual = mock.Object.Convert(value, null, null, null); What is the proper way to implement it? Erik Dietrich

Mocking a base class method call with Moq

久未见 提交于 2019-11-27 04:44:35
问题 I am modifiying a class method which formats some input paramater dates which are subsequently used as params in a method call into the base class (which lives in another assembly). I want to verify that the dates i pass in to my method are in the correct format when they are passed to the base class method so i would like to Moq the base class method call. Is this possible with Moq? 回答1: As of 2013 with latest Moq you can. Here is an example public class ViewModelBase { public virtual bool

Mock AsNoTracking Entity Framework

我与影子孤独终老i 提交于 2019-11-27 04:36:35
问题 How do I mock AsNoTracking method? In below example, DbContext has injected to the service class.It works fine if I remove AsNoTracking extension method from GetOrderedProducts method, but with AsNoTracking test fails because it returns null. I've also tried to mock AsNoTracking to return proper value but it didn't work. public interface IUnitOfWork { IDbSet<TEntity> Set<TEntity>() where TEntity : class; int SaveAllChanges(); } public class Entites : DbContext, IUnitOfWork { public virtual

Mocking generic method call for any given type parameter

寵の児 提交于 2019-11-27 04:26:33
问题 I have an interface public interface IDataProvider { T GetDataDocument<T>(Guid document) where T:class, new() } I'd like to mock it in a way, that it would just return a new instance of a given type, regardless of the exact type, something like: myMock.Setup(m => m.GetDataDocument<It.IsAny<Type>()>(It.IsAny<Guid>())) .Returns(() => new T()); (which doesn't work of course, because I cannot just give any type parameter to moq, and I can't know which type must be returned. Any ideas on this one?

How do I mock User.Identity.GetUserId()?

随声附和 提交于 2019-11-27 04:22:06
I am trying to unit test my code which includes the line: UserLoginInfo userIdentity = UserManager.GetLogins(User.Identity.GetUserId()).FirstOrDefault(); I'm just stuck on one bit as I can't get: User.Identity.GetUserId() to return a value. I have been trying the following in the set-up of my controller: var mock = new Mock<ControllerContext>(); mock.Setup(p => p.HttpContext.User.Identity.GetUserId()).Returns("string"); But it gives an error of "NotSupportedException was unhandled by user code". I have also tried the following: ControllerContext controllerContext = new ControllerContext();

What are the real-world pros and cons of each of the major mocking frameworks?

冷暖自知 提交于 2019-11-27 04:15:45
问题 see also "What should I consider when choosing a mocking framework for .Net" I'm trying to decide on a mocking framework to use on a .NET project I've recently embarked on. I'd like to speed my research on the different frameworks. I've recently read this blog post http://codevanced.net/post/Mocking-frameworks-comparison.aspx and wondered if any of the StackOverflow audience has anything to add in the way of real-world advantages and caveats to the frameworks. Could people could list the pros