moq

Moq.Mock<T> - how to set up a method that takes an expression

不羁岁月 提交于 2019-12-17 15:25:18
问题 I am Mocking my repository interface and am not sure how to set up a method that takes an expression and returns an object? I am using Moq and NUnit. Interface: public interface IReadOnlyRepository : IDisposable { IQueryable<T> All<T>() where T : class; T Single<T>(Expression<Func<T, bool>> expression) where T : class; } Test with IQueryable is already set up, but don't know how to set up the T Single: private Moq.Mock<IReadOnlyRepository> _mockRepos; private AdminController _controller;

Moq & Interop Types: works in VS2012, fails in VS2010?

此生再无相见时 提交于 2019-12-17 11:48:29
问题 I have a .NET library project with about 500 unit tests. All these tests run fine in Visual Studio 2012. However, some of my tests fail in Visual Studio 2010. In these failing tests, I use Moq to mock several Interop Types from Microsoft.Office.Interop.Excel . The test fails immediately when attempting to access these mocked interop types: Error: Missing method 'instance class Microsoft.Office.Interop.Excel.Range [ExcelAddIn.Core] Microsoft.Office.Interop.Excel.ListRow::get_Range()' from

How to mock ConfigurationManager.AppSettings with moq

雨燕双飞 提交于 2019-12-17 10:19:34
问题 I am stuck at this point of code that I do not know how to mock: ConfigurationManager.AppSettings["User"]; I have to mock the ConfigurationManager, but I don't have a clue, I am using Moq. Someone can give me a tip? Thanks! 回答1: I believe one standard approach to this is to use a facade pattern to wrap the configuration manager and then you have something loosely coupled that you have control over. So you would wrap the ConfigurationManager. Something like: public class Configuration:

What is the purpose of Verifiable() in Moq?

本秂侑毒 提交于 2019-12-17 08:55:07
问题 What is the purpose of Verifiable() ? If I verify a Mock and leave this out it still verifies the SetUp . Edit: I was using VerifyAll() thus the reason for everything being verified. After changing to Verify() only my .Verifiable() SetUp s were being checked. 回答1: ADDENDUM: As the other answer states, the purpose of .Verifiable is to enlist a Setup into a set of "deferred Verify(...) calls" which can then be triggered via mock.Verify() . The OP's clarification makes it clear that this was the

What is the purpose of Verifiable() in Moq?

别说谁变了你拦得住时间么 提交于 2019-12-17 08:54:02
问题 What is the purpose of Verifiable() ? If I verify a Mock and leave this out it still verifies the SetUp . Edit: I was using VerifyAll() thus the reason for everything being verified. After changing to Verify() only my .Verifiable() SetUp s were being checked. 回答1: ADDENDUM: As the other answer states, the purpose of .Verifiable is to enlist a Setup into a set of "deferred Verify(...) calls" which can then be triggered via mock.Verify() . The OP's clarification makes it clear that this was the

How do I mock User.Identity.GetUserId()?

↘锁芯ラ 提交于 2019-12-17 07:25:32
问题 I am trying to unit test my code which includes the line: UserLoginInfo userIdentity = UserManager.GetLogins(User.Identity.GetUserId()).FirstOrDefault(); I'm just stuck on one bit as I can't get: User.Identity.GetUserId() to return a value. I have been trying the following in the set-up of my controller: var mock = new Mock<ControllerContext>(); mock.Setup(p => p.HttpContext.User.Identity.GetUserId()).Returns("string"); But it gives an error of "NotSupportedException was unhandled by user

Why am I getting an Exception with the message “Invalid setup on a non-virtual (overridable in VB) member…”?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 05:45:11
问题 I have a unit test where I have to mock a non-virtual method that returns a bool type public class XmlCupboardAccess { public bool IsDataEntityInXmlCupboard(string dataId, out string nameInCupboard, out string refTypeInCupboard, string nameTemplate = null) { return IsDataEntityInXmlCupboard(_theDb, dataId, out nameInCupboard, out refTypeInCupboard, nameTemplate); } } So I have a mock object of XmlCupboardAccess class and I am trying to setup mock for this method in my test case as shown below

Mocking HttpClient in unit tests

泄露秘密 提交于 2019-12-17 02:59:39
问题 I have some issues trying to wrap my code to be used in unit tests. The issues is this. I Have the interface IHttpHandler: public interface IHttpHandler { HttpClient client { get; } } And the class using it, HttpHandler: public class HttpHandler : IHttpHandler { public HttpClient client { get { return new HttpClient(); } } } And then the Connection class, which uses simpleIOC to inject the client implementation: public class Connection { private IHttpHandler _httpClient; public Connection

How to Mock NamespaceManager Class Methods Using Moq?

好久不见. 提交于 2019-12-14 04:12:22
问题 https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.namespacemanager?redirectedfrom=MSDN#microsoft_servicebus_namespacemanager I want to mock CreateTopicAsync method. But because of the sealed nature of the class i am not able to mock the class. Any one Knows? 回答1: You can't mock a sealed class. Mocking relies on inheritence to build on the fly copies of the data. So trying to mock a sealed class is impossible. So what do I do? What you can do is write a wrapper: public class

Is this the correct way to use and test a class that makes use of the factory pattern?

爷,独闯天下 提交于 2019-12-14 03:42:26
问题 I don't have a lot of experience with the factory pattern and I've come across a scenario where I believe it is necessary but I'm not sure the I've implemented the pattern correctly and I'm concerned about the impact it's had on the readability of my unit tests. I've created a code snippet that approximates (from memory) the essence of the scenario I am working on at work. I'd really appreciate it if someone could take a look at it and see if what I've done seems reasonable. This is the class