Questions tagged [moq-3]

Moq (pronounced "Mock-you" or just "Mock") is a mocking framework for .NET that makes heavy use of lambdas and LINQ expression trees. This tag is specific to Moq version 3.0.

36 questions
2
votes
2 answers

How would I refactor out a static method so I can test my method?

I know I can't use Moq to mock out a static method call inside my method under test, so what would I need to do to refactor the method so I can test it? I also have a method calling the base class method, would I need to refactor that and if so how?…
chuckd
  • 13,460
  • 29
  • 152
  • 331
2
votes
1 answer

Mocking the behaviour of operators in Moq C#

Hope your well. I am in the process of creating some tests using Moq in C#. One of the objects I am Mocking has overridden ==, > and < operators. Does anyone know if its possible, and if so how to... configure a Mock object to replicate this. The…
Nick
  • 948
  • 2
  • 12
  • 24
1
vote
2 answers

Moq: Can I swap between mocked member/property behavior and unmocked member/property behavior?

I want to use one instance of a mock library I am using in a test class, however, for some tests, I may want to mock one of the member functions to do/return mocked behavior/returned value; for other tests, I may want the unmocked (native)…
gangelo
  • 3,034
  • 4
  • 29
  • 43
1
vote
1 answer

MOQ Exception Was unhandled by user

I am pretty new to MOQ and I am in learning right now. I got task that I need to write unit test But I am getting the below error which I couldn't able to find. Please anyone help me with this. Also please review the code and give me some advises…
crony
  • 491
  • 5
  • 14
  • 25
1
vote
1 answer

How to write unit tests for repository layer in domain driven design architecture using moq framework

How to write unit tests for repository layer in domain driven design architecture using moq framework? My Repository class is as following. public class ContactRepository : Repository, IContactRepository { public…
user3151024
  • 39
  • 1
  • 6
1
vote
1 answer

Dangerous to use Class Scoped vars in TestMethods

I am new to both MOQ and TDD / Unit Testing. I find myself repeating a lot of the same code in my "Arrange" section of each test method and it seems there really ought to be a better way. First the code that get's oft repeated: #region Arrange …
GPGVM
  • 5,515
  • 10
  • 56
  • 97
1
vote
1 answer

Expect multiple calls to method

How can I tell Moq to expect multiple calls so I can still use the MockRepository to VerifyAll, as below? [TestFixture] public class TestClass { [SetUp] public void SetUp() { _mockRepository = new…
BanksySan
  • 27,362
  • 33
  • 117
  • 216
1
vote
2 answers

Mocking LINQ Expressions - Moq

How do I mock something that is - Expression> using Moq? I'm trying to mock a call to my repo layer that takes in a LINQ Expression for constructing a query. I'm trying the below syntax but it fails. The SearchFor method doesn't get called. var…
govin
  • 6,445
  • 5
  • 43
  • 56
1
vote
1 answer

My Moq mock isn't being accepted in my method under test

When I create my moq mock and try to pass it into my class constructor I get this message: Argument type Moq.Mock<...mockIAppCache> is not assingable to paramter type 'IAppCache'. I included the library and I can find the reference to Mock() ok. Am…
chuckd
  • 13,460
  • 29
  • 152
  • 331
1
vote
1 answer

Matching reference type parameters by Moq setup

I have similar problem to the one asked here or here but the solutions seems not apply in my case. Consider the following classes: public interface IProductsWebService { ICObjectLang[] GetClassLevel(getClassLevelLang criteria); } // the rest of…
Marek
  • 1,688
  • 1
  • 29
  • 47
1
vote
1 answer

How do I exclude a method call from verification?

I am using Moq and set up some expectations in the TestInitialize method as so: [TestInitialize] public void init() { mockRepo.Setup(x => x.EventDefinitions).Returns(ListsOfEvents.EventDefinitions); mockRepo.Setup(x =>…
BanksySan
  • 27,362
  • 33
  • 117
  • 216
0
votes
0 answers

How to inject depdency while mocking protected method using AutoMock XNnit?

In the below code snippet, I am trying to mock the response of a protected method and test the response of the public method which calls the protected method. The protected method calls an external API, hence I would like to mock the response in…
HobbyLobbyVS
  • 61
  • 1
  • 10
0
votes
1 answer

Mock Testing a class in C# with Moq

I want to mock the GreetingMessageHandler class in C# with Moq and Nunit in Visual Studio 2019 (version 16.8.4) public class GreetingMessageHandler : AbstractServerMessageHandler { public…
Peter
  • 21
  • 5
0
votes
0 answers

How to write test case using Moq and Xunit for the following code?

Need to write the test case for the following code using moq and xunit. return null is not covered in code coverage public static StreamReader GetStream(System.Reflection.Assembly assembly, string name) { foreach (string resName in…
Taufik Shaikh
  • 259
  • 1
  • 3
  • 9
0
votes
0 answers

Return Null reference by mocking UserManger and Identity extension functions from Moq framework

I am implementing Unit testing using Nunit and moq mocking framework. While testing I successfully mocked Usermanager and identity but unable to return correct value and data type. Mock User Manager var users = new List { new MstUser {…