Click here to Skip to main content
15,887,683 members
Everything / Moq

Moq

Moq

Great Reads

by Akram El Assas
TDD in C# through a simple example
by Sem Shekhovtsov
The how-to article that summarises best practices for unit testing ASP NET Core MVC controller using NUnit and Moq framework
by Gaston Verelst
Mocking functionality using MOQ
by Jason Sultana
Common practice that I employ in test projects - re-usable mocks

Latest Articles

by Akram El Assas
Microservices sample architecture using ASP.NET Core, Ocelot, MongoDB and JWT
by Akram El Assas
TDD in C# through a simple example
by Jason Sultana
Common practice that I employ in test projects - re-usable mocks
by freedeveloper
How to create a data access class in a way, that is possible to execute unit test on it

All Articles

Sort by Score

Moq 

23 Mar 2024 by Akram El Assas
TDD in C# through a simple example
8 Apr 2018 by Sem Shekhovtsov
The how-to article that summarises best practices for unit testing ASP NET Core MVC controller using NUnit and Moq framework
3 Nov 2015 by Andy Lanng
It looks like you have two versions of the interface. One in VoiceElements.Interface and another in IVR.VoiceElementsEngine. To be able to cast you must implement the same instance of the interface.I had a similar problem when I was loading a single interface from a loaded assembly as well...
11 Aug 2020 by F-ES Sitecore
var sourceText = "abcdefg"; var index = 0; var myMock = new Mock(); myMock.Setup(m => m.NextChar()).Returns(() => sourceText[index++]); var result = myMock.Object.NextChar(); // returns "a"; result = myMock.Object.NextChar(); // returns...
10 Jan 2023 by DaveBlack
In your test, you are mocking the concrete class 'VoiceElementsLine'. You should be mocking interfaces - not concrete classes (though you are able to do so in Moq if the concrete class has a public default constructor). So, in your test you...
3 Nov 2014 by joe mery
I believe thisModels (POCO), Entity Framework and Data Patterns.is clean and neat implementation but how do you unit test you UOW Applicant.GetAll() using moq. I'm new to moq and get confuse about Unit testing all the UOW repository calls.
10 Jan 2023 by zhshqzyc
I have a class. public class VoiceElementsLine : ILine { public Guid LineId { get; set; } public TelephonyServer TelephonyServer { get; set; } public virtual ChannelResource ChannelResource { get; set; } public LanguageData LangInfo { get; set; } ...
15 Oct 2015 by Jesús Álvarez
you need to implement all mehtos of teh interface, public void Hangup(){} is not implement yet
9 Nov 2015 by zhshqzyc
Say I have the class.public class FundsTransferAuthorization { public decimal MaxAmount { get; set; } public decimal MinAmount { get; set; } private readonly IPromptPlayer _promptPlayer; public FundsTransferAuthorization(IPromptPlayer...
9 Nov 2015 by Gautham Prabhu K
You cannot Mock a class so you may need to inject the configuration via constructor and redesign the class and its usage accordingly.
19 Nov 2015 by zhshqzyc
I have a POCO class public class FundsTransferInfo { public string SiteId { get; set; } public string UserId { get; set; } public string Password { get; set; } public string TelephoneId { get; set; } public string BillToNumber { get; set; } public Decimal...
8 Dec 2015 by pratik015
I am writing a unit test case for my application.In my application, I am using SQLite datebase.I want to mock following methods.Like,ExecuteNonQuery, ExecuteScalar, GetDataSet.I am writing my methods below.Please, any one help me to write mock unit test case for following...
8 Dec 2015 by F-ES Sitecore
That code isn't really unit-testable. When you unit test you have to convert your code into services that are accessed via interfaces and it is those services\interfaces that you moq. So create an IMyData interface with a GetJobs method, then create a MyData class that implements this...
14 Mar 2016 by Gaston Verelst
Mocking functionality using MOQ
17 Jul 2016 by gaurav.s23
Error: Error activating IProductRepositoryNo matching bindings are available, and the type is not self-bindable.Activation path: 2) Injection of dependency IProductRepository into parameter productRepository of constructor of type ProductController 1) Request for...
24 Nov 2016 by Silver Lightning
Hi to all,I have to a question on how could I call a method within the same method inside my mockrespository.setup on MOQ. Below is my actual code where I need to call my method 'ChangeFilterAttribute inside the same method of 'ChangeFilterAttribute'.Or any one can give me asample...
14 Nov 2016 by #realJSOP
I would think you would have to write a recursive method that wraps the call to ChangeFilterAttribute.
24 Nov 2016 by Silver Lightning
Solved!private void GetFilters_Initialize() { var myFilters = new Object(); _mockRepository.Setup(p => p.ChangeFilterAttribute(It.IsAny>(), It.IsAny>>>())) ...
20 Apr 2017 by Vishal Pand3y
I'm trying to setup Unit Test in services using Moq and NUnit I tried Mocking context and DbSet like var mockSet = new Mock>(); var mockContext = new Mock(); mockContext.SetupGet(m => m.Student).Returns(mockSet.Object); but my services depends on other service...
2 Apr 2018 by Jamie888
Hi, may I know is there any tool/library/generator that i can refer to in order to generate general unit test method? For example, i have a method that goes to DB to retrieve data, i need to ensure that method does not return null. For traditionally way, i need to code a unit test to check for...
19 Dec 2018 by Iris Shing
I'm writing APIs using c#.Net Core One of my APIs is used to generate captcha then store to the session, I'm using Redis to store session here. And now I'm trying to write a unit test for the APIs. Cause the login API needed to pass captcha, so I need to get the captcha value from the session....
20 Jun 2019 by MAN CHUN LIEW
The sourceFile will pass in the path of the file and in destination which is the copy funciton will copy the sourceFile to it. public bool CopyFile(string sourceFile, string destinationFile) { if (File.Exists(sourceFile)) { ...
20 Jun 2019 by F-ES Sitecore
That code can't be unit tested. What is it you're actually testing? File.Exists? File.Copy? Those are third-party functions, you should just assume that they work. To make that code testable you'll need to create some kind of IFileManagement service that has Exists and Copy methods on it. ...
16 Apr 2020 by Member 14803912
Please help me how do I write a test for the following controller without changing the program code ??? ***ProductController:*** [HttpPost] [ValidateAntiForgeryToken] public async Task CreateProductAsync(ProductDTO objProduct) { ...
16 Apr 2020 by F-ES Sitecore
You can't, if you want your code to be unit testable you have to write it with unit testing in mind.
11 Aug 2020 by C Pottinger
Hi there, gurus. I'm sure a lot of you were thinking 'Thank goodness THAT guy disappeared', but I'm here to disappoint you. My question this time about using Moq with C# to create a mock that will return each letter of a supplied string each...
10 Sep 2022 by lalit.mca2006
Hi, I am using below code to upload file(IformFile) using swagger. I came to know that in new framework we must implement IOperationFilter . Here is my code: using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; ...
30 Mar 2023 by Virendra S from Bangalore, Karnataka
GetAbsoluteExpirationValue() SonarQube report says, this method is partially covered(Partially covered by tests, 1 of 2 conditions). But Resharper shows 100 % coverage. Can you help me what am I missing here? public MemoryCacheEntryOptions...
30 Mar 2023 by Richard Deeming
ReSharper is wrong. You test what happens when the configuration contains an invalid value; but you never test what happens when the configuration contains a valid value. There are four paths you need to test: ...
5 Aug 2023 by Jason Sultana
Common practice that I employ in test projects - re-usable mocks
23 Mar 2024 by Akram El Assas
Microservices sample architecture using ASP.NET Core, Ocelot, MongoDB and JWT
7 Mar 2016 by Akhil Mittal
In this article we’ll learn on how to write unit tests for WebAPI controllers i.e. REST’s actual endpoints.
14 Oct 2014 by Mario Majčica
When and how to use the Callback method on Moq in your unit tests.
1 Mar 2016 by Akhil Mittal
In this article we learn how to write unit tests for core business logic and primarily on basic CRUD operations.
10 Aug 2016 by Aless Alessio
Design your solution and code your classes as loosely-coupled objects. Learn how to use MOQ and Ninject for mocking your Service and injecting it at runtime with Ninject.
8 May 2017 by Habibur Rony
BDD, AAA Structure And Object Mocking in Unit testing
23 Jul 2017 by Praveenkj79
This article describes how to test your .NET core API using MS Test.
16 May 2022 by freedeveloper
How to create a data access class in a way, that is possible to execute unit test on it
15 Sep 2015 by Moe Farag
While writing tests for methods that use .NET Reflection, I found out that Rhino doesn't expose stubbed method for reflected invocation.
26 May 2020 by Daan Acohen
Some functionality of Moq can be really important but can also be easily forgotten as explained here
15 Oct 2015 by Jesús Álvarez
I supose your class ChannelResource dont have a default constructor or empty, public void ChannelResource(){}
2 Apr 2018 by F-ES Sitecore
First off if it needs a database it isn't a unit test; unit tests are to check your logic, not what data you have in a database or connectivity. Those kind of tests are integration tests and will be done elsewhere, probably as a separate process. Unit tests should be able to run when the...
17 Jul 2016 by gaurav.s23
I change the statements with fully qualified name and it worked. But i did not understand even though i have used using statements on top with Namespaces Why it did not work??In ninjectWebCommon.Cs file:private static void RegisterServices(IKernel...
30 Oct 2014 by suma_sarur
Hi,I have to automate my already existing (production) application, which is implemented using Visual Studio 2010.I am looking for some free (open source) Mocking tool which supports for Non Virtual members, b'coz my application does not have any virtual members and I can't change my...
3 Nov 2015 by zhshqzyc
I have an interface:namespace IVR.VoiceElementsEngine{ public interface IVoiceResource { void ClearDigitBuffer(); }}And the implementation:namespace IVR.VoiceElementsEngine{ public class VoiceElementsResource:IVoiceResource { private...