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
1 answer

Mock ApplicationUser and provide roles

I am trying to supply an ApplicationUser to my mocked ApplicationUserManager. When I try and add roles I notice that Roles is a read-only property. Not sure the best way to implement in my test. [TestMethod] public void…
CBC_NS
  • 1,961
  • 4
  • 27
  • 47
3
votes
2 answers

How to Unit Test Asp.Net MVC Identity UserManager.ChangePasswordAsync

I am writing unit Test cases for my Home Controller, in Home controller i have Action Method called MyProfile which calls ChangePasswordAsync Method in UserManager class. How to test application User manager Below is my Controller…
Charan Tej
  • 61
  • 5
3
votes
2 answers

Using Moq to see if method was called with a value

Using Microsoft Visual Studio 2017's built in test with Moq. I have a simple class that creates some content, then sends the content to a notification system. I need to test if the notification system was called, but that the call included some…
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
3
votes
1 answer

Mock Entity Framework long LINQ query

Have a LINQ query expression (below) that want to test mocking the Entity Framework. To mock I am using the code: Entity Framework Testing with a Mocking Framework This works for other queries but doesn't work for this var query = from vwPs in…
3
votes
1 answer

Unit test ServiceStack services in ServiceStack 3.9.71

I recently took a .Net project over which exposes DAOs from a Microsoft SQL Database via ServiceStack(3.9.71) REST API. Since I am gonna refactor some parts I want to unit test (at least) all servicestack services. For a better understanding I…
zlZimon
  • 2,334
  • 4
  • 21
  • 51
3
votes
3 answers

How to mock StoredProcedureResponse

The question is pretty simple. I've Moq'ed an IDocumentClient (which is the main DocumentDB .NET client). I am Moq'ing the ExecuteStoredProcedureAsync() method. It returns a StoredProcedureResponse type (a concrete type), but its interface is very…
Jmoney38
  • 3,096
  • 2
  • 25
  • 26
3
votes
1 answer

Mock Interface casting back to original class

My MongoDbRepository has an interface called IRepository. So I am mocking the interface to set up the method which is UpdateOneAsync. However my MerchantConfigurationRepository can only take a MongoDbRepository object which is why I need to cast it.…
Lostaunaum
  • 697
  • 1
  • 10
  • 31
3
votes
2 answers

Issue using Moq in Asp.net Core:Invalid setup on a non-virtual (overridable in VB)

I write a unit test using xunit and Moq in asp.net core, i follow this Article for this, but i got this error: Invalid setup on a non-virtual (overridable in VB) member: m => m.Blogs this is what i tried: [Fact] public void CreateBlog() …
pmn
  • 2,176
  • 6
  • 29
  • 56
3
votes
2 answers

How to handle Invalidoperationexception when run the unit test in c#

I wrote the unit test using MOQ. When setup the mock I am trying to create the object of class in Linq query. After that I am trying to run the Unit test, but I get below error message. "When called from 'VisitMemberInit', rewriting a node of type…
venkat
  • 85
  • 2
  • 13
3
votes
0 answers

How test this code with Moq and AutoMapper

I have no experience on writting Unit Tests and I am starting with it. My project has a Data Access Layer (with nhibernate) where I have repositories that are injected by an IoC container into some Services in a Business Layer. I want to write Unit…
Karl
  • 31
  • 4
3
votes
1 answer

Is there an Autofixture attribute that can be used instead of fixture.Inject() in the code?

I am creating tests against an Entity Framework 6 repository. Due to a dependency between a product type and loan, I get a circular reference error when using Autofixture.AutoMoq. Is there an attribute that I can place over a test method so I can…
A Bit of Help
  • 1,368
  • 19
  • 36
3
votes
3 answers

Writing C# nunit test without having access to the rest of the code

I have a class Person and the interface: interface IService { double GetAccDetails(int personId); } public class Person { private int _personId; private IService _service; public Person(int personId, IService service) { …
J.Doe
  • 143
  • 1
  • 2
  • 5
3
votes
2 answers

How to mock method which accepts out parameter

I'm using moq and would like to mock a method which accepts out parameter: protected void GetDataRow(string id, out DataRow dataRow) This is what I tried: dataMock.Protected().Setup("GetDataRow", ItExpr.IsAny(),…
Bart
  • 294
  • 1
  • 2
  • 10
3
votes
2 answers

Even simple Moq code is throwing NotSupportedException

I've been struggling to use Moq as a mocking framework and copied some very simple example code. I must be missing something really stupid here. It throws a NotSupportedException on the Setup call, even though it points to the Returns method. This…
MrFox
  • 4,852
  • 7
  • 45
  • 81
3
votes
1 answer

Moq - Accessing the mocked object's properties

Eg: I have an interface with a property and a method. The method does something with the value of the property. How do I setup the mock to access the value of property? interface myInterface { Id{get;set;} string…
Raj Rao
  • 8,872
  • 12
  • 69
  • 83
1 2 3
99
100