moq

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

依然范特西╮ 提交于 2019-11-27 03:14:14
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 [TestMethod] Public void Test() { private string temp1; private string temp2; private Mock

How to Verify another method in the class was called using Moq

时间秒杀一切 提交于 2019-11-27 02:44:53
问题 This seems like something simple but I can't seem to get it to work. I have a class with a Save method that simply calls another method ShouldBeCalled(). I want to verify that if I call Save() that the other method ShouldBeCalled() is executed at least once. I thought that I could do the following. public class ClassA { public virtual void Save() { ShouldBeCalled(); } public virtual void ShouldBeCalled() { //This should get executed } } [TestFixture] public class ClassA_Test { [Test] public

Mocking internal classes with Moq for unit testing

风格不统一 提交于 2019-11-27 02:11:53
问题 Say I have a class "ClassA", which has a dependency on a class "ClassB" (injected into the constructor of ClassA). I want to mock ClassB so that I can test ClassA in isolation. Both classes are internal. Correct me if I'm wrong but it looks like Moq can only mock a class if it is public, it has a public parameterless constructor, and the methods to be mocked are public virtual . I don't really want to make these classes publicly visible. Am I missing something with Moq, or is it just not

Mocking objects with Moq when constructor has parameters

Deadly 提交于 2019-11-27 01:43:19
问题 I have an object I'm trying to mock using moq. The object's constructor has required parameters: public class CustomerSyncEngine { public CustomerSyncEngine(ILoggingProvider loggingProvider, ICrmProvider crmProvider, ICacheProvider cacheProvider) { ... } } Now I'm trying to create the mock for this object using either moq's v3 "setup" or v4 "Mock.Of" syntax but can't figure this out... everything I'm trying isn't validating. Here's what I have so far, but the last line is giving me a real

Mocking an NHibernate ISession with Moq

你。 提交于 2019-11-27 01:31:21
问题 I am starting a new project with NHibernate, ASP.NET MVC 2.0 and StructureMap and using NUnit and Moq for testing. For each of my controllers I have a single public constructor into which an ISession is being injected. The application itself works just fine, but in terms of unit testing I essentially have to mock an ISession in order to test the controllers. When I attempt to Mock the ISession with MOQ i get the following error message: Only property accesses are supported in intermediate

Mocking generic methods in Moq without specifying T

一曲冷凌霜 提交于 2019-11-27 01:27:32
问题 I have an interface with a method as follows: public interface IRepo { IA<T> Reserve<T>(); } I would like to mock the class that contains this method without having to specify Setup methods for every type it could be used for. Ideally, I'd just like it to return a new mock<T>.Object . How do I achieve this? It seems my explanation was unclear. Here's an example - this is possible right now, when I specify the T (here, string): [TestMethod] public void ExampleTest() { var mock = new Mock<IRepo

Mocking new Microsoft Entity Framework Identity UserManager and RoleManager

孤街浪徒 提交于 2019-11-27 01:18:53
问题 Has anyone come up with a successful mocking solution for UserManager and RoleManager ? I have been beating my head against a wall all day. All I want to do is mock the objects to use an in memory collection rather than hitting the Entity Framework data store. I've scoured the internet and tried several different approaches using MOQ. I was under the impression that the new stuff was much easier to test. Am I missing something? 回答1: Alternatively, you can mock the IUserStore<TUser> interface

Verifying a method was called

和自甴很熟 提交于 2019-11-27 01:18:25
问题 Using Moq, I have a very odd issue where the setup on a mock only seems to work if the method I am setting up is public. I don't know if this is a Moq bug or if I just have this wrong (newbie to Moq). Here is the test case: public class TestClass { public string Say() { return Hello(); } internal virtual string Hello() { return ""; } } [TestMethod] public void Say_WhenPublic_CallsHello() { Mock<TestClass> mock = new Mock<TestClass>(); mock.Setup(x => x.Hello()).Returns("Hello World"); string

How to throw a SqlException when needed for mocking and unit testing?

房东的猫 提交于 2019-11-27 01:11:53
问题 I am trying to test some exceptions in my project and one of the Exceptions I catch is SQlException . It seems that you can't go new SqlException() so I am not sure how I can throw an exception especially without somehow calling the database (and since these are unit tests it is usually advised not to call the database since it is slow). I am using NUnit and Moq, but I am not sure how to fake this. Responding to some of the answers that seem to all be based on ADO.NET, note that I am using

Moq: unit testing a method relying on HttpContext

╄→尐↘猪︶ㄣ 提交于 2019-11-27 00:59:25
Consider a method in a .NET assembly: public static string GetSecurityContextUserName() { //extract the username from request string sUser = HttpContext.Current.User.Identity.Name; //everything after the domain sUser = sUser.Substring(sUser.IndexOf("\\") + 1).ToLower(); return sUser; } I'd like to call this method from a unit test using the Moq framework. This assembly is part of a webforms solution. The unit test looks like this, but I am missing the Moq code. //arrange string ADAccount = "BUGSBUNNY"; string fullADName = "LOONEYTUNES\BUGSBUNNY"; //act //need to mock up the HttpContext here