Questions tagged [moq]

Moq is a strongly typed and minimalistic mocking framework for .NET.

Moq (pronounced "Mock-you" or just "Mock") is the only mocking library for .NET developed from scratch to take full advantage of .NET Linq expression trees and lambda expressions, which makes it the most productive, type-safe and refactoring-friendly mocking library available. It supports mocking interfaces as well as classes. Its API is extremely simple and straightforward, and doesn't require any prior knowledge or experience with mocking concepts.


Resources

Project code

Wiki

Quick start

Installing Moq can most easily be done using its NuGet package:

Install-Package Moq
5720 questions
3
votes
6 answers

How to mock Autofaq interface with Moq, when an extension method is called

Using C#/Autofac/Moq: I have this class: public class MyService { private readonly IlifetimeScope _scope; public MyService(ILifetimeScope scope) { _scope = scope; } public void DoWork() { var dbContext =…
Henrik Oscarsson
  • 147
  • 1
  • 11
3
votes
1 answer

unit testing with moq generic repository and UnitOfWork

I have a specific and generic repository like that : Generic repository: public class Repository : IRepository where TEntity : class { protected readonly DbContext Context; private DbSet _entities; public…
Omar AMEZOUG
  • 958
  • 2
  • 14
  • 38
3
votes
2 answers

How to mock chained Linq methods in Moq

I have a query that goes var myRecord = myRepository.All().Any(e => e.Id == id); I mocked it with this this._myMockRepository .Setup(s => s.All().Any(It.IsAny>>())) .Returns(this.GetFakeMyObjects().Any(t =>…
Neil Humby
  • 253
  • 4
  • 15
3
votes
1 answer

Moq ConfigurationSection

my application has the following code: public interface IConfigurationManager { CustomSection Settings { get; } } public class ConfigurationManager : IConfigurationManager { public CustomSection Settings { get { return…
nfplee
  • 7,643
  • 12
  • 63
  • 124
3
votes
1 answer

How to use moq to unit test an application that consumes asmx service in .NET

I have a web application that sends an sms by consuming an ASMX service(which is now production and working fine), however I now need to unit test this application consumes the asmx service, below is the specific action method I'm interested in unit…
Mronzer
  • 399
  • 1
  • 7
  • 18
3
votes
3 answers

how to mock a method call using moq

My unit testing method is as follows [Test] public void TrackPublicationChangesOnCDSTest() { //Arrange // objDiskDeliveryBO = new DiskDeliveryBO(); //Act var actualResult =…
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86
3
votes
1 answer

Moq dotnetcore - cannot convert from 'method group' to 'Expression>'

I'm trying to setup Moq in a dotnetcore project. I have a generic repository that is called from one of my controllers. I would like to test this an is using moq to setup the call. But I get an error. The generic repository call that is initiated…
Crytrus
  • 751
  • 1
  • 8
  • 14
3
votes
4 answers

Mocking FindAsync method

I mocked FindAsync by the following code: var brands = new Mock>(); ConfigureTheDbSet(brands, brandData); brands.Setup(b => b.FindAsync(It.IsAny())) //substitution of the .SelectAsync(id) method …
Dzmitry Shauchuk
  • 336
  • 2
  • 17
3
votes
1 answer

Moq a concrete class method call

I've got a setup like this with a concrete class that is instantiated inside the method I want to test. I want to mock this concrete class an not have it execute the code inside. Hence, no exception should be thrown: public class Executor { …
Rebecca
  • 13,914
  • 10
  • 95
  • 136
3
votes
3 answers

Moq, Ninject, Fluent NHibernate styled library?

This is a brain storming question. Any comments would be appreciated. Recently I've observed a few source frameworks with a pattern similar to each other. Moq mock.Setup(framework => framework.DownloadExists("2.0.0.0")) .Returns(true) …
Shuo
  • 4,749
  • 9
  • 45
  • 63
3
votes
2 answers

Testing an async method in ASP MVC Controller with Nunit

I have an ASP.NET MVC application, with an Controller that features asynchronous methods, returning Task object and marked with the async keyword. This method only takes the data from the database in async mode. public async…
Titov S.P.
  • 75
  • 1
  • 7
3
votes
1 answer

Underlying type of a mock object

I need to check the type of a mock object and want to get the underlying type. For instance, for an object such as Mock is there anything on here I can call to get the type, "Foo"? I am using moq. Mock myFoo = new…
Joe Cartano
  • 2,997
  • 3
  • 22
  • 40
3
votes
1 answer

C# Moq - Mocking Action parameter

I've been strugling with a moq setup for a while, and I'd like get some help from you, any help is appreciated. Basic codes public interface IMessageHub { void Subscribe(string topic, Action callback); } Having message hub, where you can…
Ádám Kovács
  • 107
  • 2
  • 10
3
votes
2 answers

Making actions get called in silverlight unit test with MOQ

Lets say I have this _articlesService.SaveAsync(Model, AddressOf OnSaveCompleted) The OnSaveCompleteMethod do a couple of things, obviously. Its a Protected Overridable Sub OnSaveCompleted(ByVal asyncValidationResult As AsyncValidationResult) In…
Einarsson
  • 522
  • 2
  • 14
3
votes
3 answers

Moq an object in a static class

I can't get Moq to mock an object that gets created in a static method. Here is my moq and code code: public interface IConfigHelper { string GetConfiguration(string sectionName, string elementName); } public class ConfigHelper :…
GregJF
  • 456
  • 4
  • 14
1 2 3
99
100