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
2 answers

Unit Testing with IMongoQueryable

I am using .NET Core 2.0 and the .NET Core MongoDB driver. I have created a repository like so: public interface IRepository { IMongoQueryable Get() } I have done this to give flexibility to whoever uses this to be able to do LINQ much…
Kevin Lee
  • 1,104
  • 3
  • 13
  • 33
3
votes
1 answer

How to get value from mocked interface that has no get with Moq?

I'm trying to write some tests for a specific interface using Moq. The values of the object that implements the interface are set by a class that we will call "controller" in this question. Interface: public interface ITestInterface { int…
Th0rndike
  • 3,406
  • 3
  • 22
  • 42
3
votes
1 answer

Caliburn EventAggregator moq verify PublishOnUIThreadAsync async method

I have an event as following namespace MyProject { public class MyEvent { public MyEvent(int favoriteNumber) { this.FavoriteNumber = favoriteNumber; } public int FavoriteNumber { get; private set;…
Hussain Mir
  • 107
  • 7
3
votes
1 answer

Mock returns null value when ReturnResult is a custom object but works as expected when it is a primitive type bool

I am using Moq in .net core(1.1) and having a bit of torrid time understanding this behavior as all the examples on interweb points to the fact the this should work with no issues. I have already tried…
Walaitki
  • 165
  • 2
  • 11
3
votes
1 answer

Testing viewmodel constructor arguments with Moq

I have a basic CRUD application written in ASP.NET Core, with an example controller action looking like so: public IActionResult Sheet(Guid characterId) { var character = _repository.GetCharacter(characterId); // omit validation for brevity …
Telvee32
  • 109
  • 1
  • 7
3
votes
1 answer

How to run a function when return value is void upon setup in Moq

So when there is a return value, I can do this in Moq mockStudentRepository.Setup(m => m.Create(It.IsAny())).Returns(s => { students.Add(s); return 1; }); so this lambda gets ran as the mock implementation of the…
g_b
  • 11,728
  • 9
  • 43
  • 80
3
votes
1 answer

Using Moq : Mock object update automatically?

I am new to MoQ framework. I am writing unit testing for controller using MoQ framework and here is my test method, var mockedItemDetail = new ItemDetail() { Name = null }; var mockObject = new Mock(); …
Praveen
  • 831
  • 7
  • 15
3
votes
1 answer

Why is the ?? operator not working in Moq setup method

Could someone explain to me, why this doesn't work? builder.Setup(b => b.BuildCommand(query ?? It.IsAny())).Returns(command); If query is null, BuildCommand will be passed null, and not It.IsAny() Instead, I have to do…
Chris Wohlert
  • 610
  • 3
  • 12
3
votes
1 answer

Creating Mock reference and setup methods for complex operations using MOQ

I have two methods , OpenCertificateStore and FindCertificateBySubjectName and implemented them as following: public void OpenCertificateStore() { if (_certificateStore == default(X509Store)) _certificateStore =…
Simsons
  • 12,295
  • 42
  • 153
  • 269
3
votes
2 answers

Should this case of Assert.AreSame return true?

I am testing a repository pattern I have created and I use Moq package to mock my objects. I wanted to test references from 2 objects, but the result kind of surprises me. Here is the test: Mock>…
Flexabust Bergson
  • 732
  • 14
  • 34
3
votes
2 answers

Modifying mock property which has been set up

I'm going to simplify a bit the problem: In my tests, I use a mocked object (I mocked it because it calls a bdd) which I give in parameters to a method of another object (not mocked) whose purpose is to modify a property of this mocked…
3
votes
2 answers

AutoFixture Build-Freeze-Create sequence

I just noticed, that whenever I do Freeze on a fixture in-between Build<>()-Create() calls that Freezes do not get applied. Is it intended behavior of AutoFixture or a bug? To make things clear: var fixture = new Fixture().Customize(new…
SOReader
  • 5,697
  • 5
  • 31
  • 53
3
votes
2 answers

Moq - Check whether method is mocked (setup-ed)

We all know that in Moq, we can mock the method using Setup. How can I check if the method is being setup? I don't want to invoke the method and check its result, because that will count during Verify as an actual call to the mocked method (unless…
Tengiz
  • 8,011
  • 30
  • 39
3
votes
1 answer

Mocking ApiController SignalR broadcasting

I'm trying to mock SignalR broadcasting present in ApiController(WebApi), but unable to complete test case, below is my code SignalRHub public class HubServer : Hub { } ApiControllerWithHub public abstract class…
Shri
  • 351
  • 3
  • 16
3
votes
1 answer

Ways to mock SignalR Clients.Group in unit test

I'm writing mock test cases for SignalR application. I just started with the help of Unit Testing SignalR Applications, but my requirement is little bit different that example shown there. Following is the code I have done after…
Shri
  • 351
  • 3
  • 16