moq

Moq and throwing a SqlException

天涯浪子 提交于 2019-11-28 22:15:34
问题 I have the following code to test that when a certain name is passed to my method, it throws a SQL exception (there is reason to that one, although it sounds a little odd). mockAccountDAL.Setup(m => m.CreateAccount(It.IsAny<string>(), "Display Name 2", It.IsAny<string>())).Throws<SqlException>(); However, this won't compile because SqlException's constructor is internal: 'System.Data.SqlClient.SqlException' must be a non-abstract type with a public parameterless constructor in order to use it

How to mock ActionExecutingContext with Moq?

痴心易碎 提交于 2019-11-28 21:32:36
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 = new ActionExecutingContext( new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new

Why DbContext doesn't implement IDbContext interface?

拜拜、爱过 提交于 2019-11-28 20:46:35
问题 Why there is no IDbContext interface in the Entity Framework? Wouldn't it be easier to test things if there was an existing interface with methods like SaveChanges() etc. from which you could derive your custom database context interface? public interface ICustomDbContext : IDbContext { // add entity set properties to existing set of methods in IDbContext IDbSet<SomeEntity> SomeEntities { get; } } 回答1: I see this IDbContext : See this link And then you make a new partial class for your

How to mock ModelState.IsValid using the Moq framework?

坚强是说给别人听的谎言 提交于 2019-11-28 20:44:15
问题 I'm checking ModelState.IsValid in my controller action method that creates an Employee like this: [HttpPost] public virtual ActionResult Create(EmployeeForm employeeForm) { if (this.ModelState.IsValid) { IEmployee employee = this._uiFactoryInstance.Map(employeeForm); employee.Save(); } // Etc. } I want to mock it in my unit test method using Moq Framework. I tried to mock it like this: var modelState = new Mock<ModelStateDictionary>(); modelState.Setup(m => m.IsValid).Returns(true); But this

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

送分小仙女□ 提交于 2019-11-28 20:12:30
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? Mark Seemann Only TypeMock Isolator (and perhaps Moles) can perform these stunts. Normal dynamic mock libraries can only mock virtual and abstract members . Ariel Popovsky You should use Moq to create your Mock object and set CallBase

.NET 4, AllowPartiallyTrustedCallers attribute, and security markings like SecurityCritical

僤鯓⒐⒋嵵緔 提交于 2019-11-28 18:15:30
I'm new C# and am trying to understand the new security features of .NET-4 . To fill in some details, I'm currently trying to update AutofacContrib.Moq to work with the latest Moq. I had no problems doing this for .NET-3.5 and under. But in .NET-4 the security restrictions result in numerous security exceptions. Moq has a a single method, GetObjectData , that's marked with the SecurityCritical attribute. AutofacContrib.Moq has the AllowPartiallyTrustedCallers attribute set which is the source of the exceptions. It seems that rather than adding the SecurityRules attribute with a SecurityLevel

How do I verify a method was called exactly once with Moq?

我们两清 提交于 2019-11-28 17:41:26
How do I verify a method was called exactly once with Moq? The Verify() vs. Verifable() thing is really confusing. Jeff Ogata You can use Times.Once() , or Times.Exactly(1) : mockContext.Verify(x => x.SaveChanges(), Times.Once()); mockContext.Verify(x => x.SaveChanges(), Times.Exactly(1)); Here are the methods on the Times class: AtLeast - Specifies that a mocked method should be invoked times times as minimum. AtLeastOnce - Specifies that a mocked method should be invoked one time as minimum. AtMost - Specifies that a mocked method should be invoked times time as maximum. AtMostOnce -

Testing controller Action that uses User.Identity.Name

半世苍凉 提交于 2019-11-28 17:13:29
I have an action that relies on User.Identity.Name to get the username of the current user to get a list of his orders: public ActionResult XLineas() { ViewData["Filtre"] = _options.Filtre; ViewData["NomesPendents"] = _options.NomesPendents; return View(_repository.ObteLiniesPedido(User.Identity.Name,_options.Filtre,_options.NomesPendents)); } Now I'm trying to write unit tests for this, but I get stuck on how to provide a Mock for User.Identity.Name. If I run my test as I have it (without mock for User...), I get a Null.. exception. Which is the correct approach for this? I'm thinking that my

Mocking using Moq in c#

橙三吉。 提交于 2019-11-28 16:53:30
I have the following code: public interface IProductDataAccess { bool CreateProduct(Product newProduct); } Class ProductDataAccess implements that interface. public class ProductBusiness { public bool CreateProduct(Product newProduct) { IProductDataAccess pda = new ProductDataAccess(); bool result = pda.CreateProduct(newProduct); return result; } } In this case, how to create unit test for CreateProduct method by mocking the IProductDataAccess interface? I thought of having an public instance of IProductDataAccess within ProductBusiness and initialize it using Mock<IProductDataAccess> object

MOQ - how to mock an interface that needs to be cast to another interface?

﹥>﹥吖頭↗ 提交于 2019-11-28 16:23:56
问题 what I want to do is construct a moq for I1 - which is fine ... however in the course of the method that I am testing that uses this mock I need to cast it to I2 in order to access some properties that are not on I1 Interface I1 { int AProperty{get;set;}} Interface I2 {int AnotherProperty{get;set;}} I then have some objects Class O1 : I1 {} and Class O2 : O1 , I2 {} the problem is that when i have an instance of a I2 implementing object I can cast it to I1 in order to access the methods that