moq

Using Moq to override virtual methods in the same class

痴心易碎 提交于 2019-11-27 17:25:35
问题 We are using Moq to unit test our service classes, but are stuck on how to test situations where a service method calls another service method of the same class. I tried setting the method being called to virtual, but still couldn't figure out what to do then in Moq. For example: public class RenewalService : IRenewalService { //we've already tested this public virtual DateTime? GetNextRenewalDate(Guid clientId) { DateTime? nextRenewalDate = null; //...<snip> a ton of already tested stuff...

Moq - How to verify that a property value is set via the setter

帅比萌擦擦* 提交于 2019-11-27 17:05:10
问题 Consider this class: public class Content { public virtual bool IsCheckedOut {get; private set;} public virtual void CheckOut() { IsCheckedOut = true; } public virtual void CheckIn() { //Do Nothing for now as demonstrating false positive test. } } The Checkin method is intentionally empty. Now i have a few test methods to verify the status of calling each method. [TestMethod] public void CheckOutSetsCheckedOutStatusToTrue() { Content c = new Content(); c.CheckOut(); Assert.AreEqual(true, c

How to write this EF Mock setup code as a reusable Generic Boilerplate?

主宰稳场 提交于 2019-11-27 16:13:19
I am using moq, ef 6 and xunit. I find myself writing this code over and over and thought maybe I could make it into a generic method but having some trouble. public static void CreateSalesMock(List<Sale> sales, Mock<DatabaseContext> dbContextMock) { var data = sales.AsQueryable(); var mockSet = new Mock<DbSet<Sale>>(); mockSet.As<IQueryable<Sale>>() .Setup(x => x.Provider) .Returns(data.Provider); mockSet.As<IQueryable<Sale>>() .Setup(x => x.Expression) .Returns(data.Expression); mockSet.As<IQueryable<Sale>>() .Setup(x => x.ElementType) .Returns(data.ElementType); mockSet.As<IQueryable<Sale>>

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

元气小坏坏 提交于 2019-11-27 14:25:46
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 class 'Castle.Proxies.ListRowProxy'. This exception implies that I forgot to setup the appropriate property

How to mock ActionExecutingContext with Moq?

别等时光非礼了梦想. 提交于 2019-11-27 13:16:01
问题 I am trying to test the following filter: using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Filters; namespace Hello { public class ValidationFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.ModelState.IsValid) { filterContext.Result = new BadRequestObjectResult(filterContext.ModelState); } } } } I am trying to mock the ActionFilterAttribute using Moq. I am using Mvc 6.0.0-rc1 My first try: var context =

How do I Moq a method that has an optional argument in its signature without explicitly specifying it or using an overload?

无人久伴 提交于 2019-11-27 12:57:52
Given the following interface: public interface IFoo { bool Foo(string a, bool b = false); } Attempting to mock it using Moq: var mock = new Mock<IFoo>(); mock.Setup(mock => mock.Foo(It.IsAny<string>())).Returns(false); gives the following error at compile time: An expression tree may not contain a call or invocation that uses optional arguments I've found the issue above raised as an enhancement in Moq's list of issues and it appears to be assigned to the 4.5 release (whenever that is). My question is: what should I do given that the above is not going to be fixed anytime soon? Are my options

How to mock a function call on a concrete object with Moq?

不打扰是莪最后的温柔 提交于 2019-11-27 12:47:18
问题 How can I do this in Moq? Foo bar = new Foo(); Fake(bar.PrivateGetter).Return('whatever value') It seems I can only find how to mock an object that was created via the framework. I want to mock just a single method/property on a concrete object I've created. In TypeMock, I would just do Isolate.WhenCalled(bar.PrivateGetter).Returns('whatever value') . Any ideas? 回答1: Only TypeMock Isolator (and perhaps Moles) can perform these stunts. Normal dynamic mock libraries can only mock virtual and

What is the purpose of Verifiable() in Moq?

守給你的承諾、 提交于 2019-11-27 11:51:48
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. Ruben Bartelink 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 goal and the only problem was figuring out why it wasn't working, but as @Liam prodded, the

Moq, strict vs loose usage

独自空忆成欢 提交于 2019-11-27 11:27:54
问题 In the past, I have only used Rhino Mocks, with the typical strict mock. I am now working with Moq on a project and I am wondering about the proper usage. Let's assume that I have an object Foo with method Bar which calls a Bizz method on object Buzz. In my test, I want to verify that Bizz is called, therefore I feel there are two possible options: With a strict mock var mockBuzz= new Mock<IBuzz>(MockBehavior.Strict); mockBuzz.Setup(x => x.Bizz()); //test will fail if Bizz method not called

How to mock ConfigurationManager.AppSettings with moq

一个人想着一个人 提交于 2019-11-27 11:09:47
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! Joshua Enfield 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: IConfiguration { public User { get{ return ConfigurationManager.AppSettings["User"]; } } } (You can