moq

ASP.Net Core unit testing async controller [duplicate]

元气小坏坏 提交于 2019-12-02 00:35:23
This question already has an answer here: How to mock an async repository with Entity Framework Core 4 answers I have this test: [Fact] public async void Can_Paginate() { //fake data var product1 = new Product { ProductId = 1, ProductName = "P1" }; var product2 = new Product { ProductId = 2, ProductName = "Product 2" }; var product3 = new Product { ProductId = 3, ProductName = "Product 3" }; var product4 = new Product { ProductId = 4, ProductName = "Product 4" }; var product5 = new Product { ProductId = 5, ProductName = "Product 5" }; var data = new List<Product> { product1, product2, product3

IOC on IValidationDictionary with Castle Windsor

狂风中的少年 提交于 2019-12-02 00:08:28
I'm new to Castle Windsor and am just using the latest version. I've created entries for my repositories which are working fine but I have one final dependency that I'm passing into my controller. I've created a ModelStateWrapper which inherits from IValidationDictionary. The ModelStateWrapper takes a ModelStateDictionary in it's constructor so that in my code I can pass the following as an example: IMembershipService _memSvc; IValidationDictionary _validationService; public AccountController() { _validationService = new ModelStateWrapper(this.ModelState); _memSvc = new MembershipService(

MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action

旧城冷巷雨未停 提交于 2019-12-01 22:48:06
Here's my problem - I am using MvcMailer to create nicely formatted emails using Razor syntax, and it's a great tool to use for that! The issue I'm running into is this - here's some syntax from my View for one of the emails i send: <p>Click here to return to <a href="@Url.Abs(Url.Action("Details", "Home", new{ Id=ViewBag.IdeaId}))">@ViewBag.IdeaName</a></p> Whenever I try to run my unit tests, I get the following error message: Can we send out email notifications for new comments?: System.ArgumentNullException : Value cannot be null. Parameter name: httpContext Stacktrace - shortened for

Mocking generics that implement multiple interfaces

删除回忆录丶 提交于 2019-12-01 22:47:38
问题 Here's my class implementation where the generic is implementing two interfaces... public class ClassA<TGeneric> : where TGeneric: IInterfaceA, IInterfaceB I want to Mock ClassA. However, I can't use var mock = new Mock<Class<A<IInterfaceA>>(); or var mock = new Mock<Class<A<IInterfaceB>>(); since the generic requires implementations of both interfaces. I know you can mock objects with multiple interfaces by using the As() method on the moq, but I don't really have an object here but a

Verify that either one method or the other was invoked in a unit test

折月煮酒 提交于 2019-12-01 22:10:22
Example: public bool Save(MyObj instance) { if (instance.IsNew) { this.repository.Create(instance); } else { this.repository.Update(instance); } } How do I create a test in Moq that verifies: that a property IsNew is being read that either Create() or Update() has been invoked Off the top of my head: Verifying that the IsNew property is being read: var mock = new Mock<MyObj>(); mock.Setup(m => m.IsNew).Returns(true).Verifiable(); //... sut.Save(mock.Object); //... mock.Verify(); In the example above, the IsNew property will return true , so the Create path will be taken. To verify that either

Unit test to verify that a base class method is called

限于喜欢 提交于 2019-12-01 21:59:15
I have a base class: public abstract class MyBaseClass { protected virtual void Method1() { } } and a derived class: public class MyDerivedClass : MyBaseClass { public void Method2() { base.Method1(); } } I want to write a unit test for Method2 to verify that it calls Method1 on the base class. I'm using Moq as my mocking library. Is this possible? I came across a related SO link: Mocking a base class method call with Moq in which the 2nd answer suggests it can be achieved by setting CallBase property to true on the mock object. However it's not clear how this would enable the call to the base

Unable to mock Owin context in C# WEB API REST service unit test

孤街浪徒 提交于 2019-12-01 21:43:26
I am trying to mock the OWIN context in a C# WEB API REST service but the OWIN context always seems to be null. I am using moq and .NET framework 4.5.2. Here is the controller method I am trying to test: public class CustomerController : ApiController { // GET: api/Customer/n [HttpGet] [ClaimsPrincipalPermission(SecurityAction.Demand, Operation = Actions.View, Resource = Resources.Self)] public IHttpActionResult Get(int id) { if (id <= 0) return BadRequest(); ClaimsPrincipalPermission.CheckAccess(id.ToString(),"UserId"); string jsonResponse = string.Empty; HttpStatusCode returnCode = this

Mocking insert query to a MySQL Database using Moq

我与影子孤独终老i 提交于 2019-12-01 21:21:39
I am currently trying to learn Mocking with Moq, and I wanted to try it on an existing database that I have, however I am unsure how is the correct way to approach this. In my data layer I have a class that handles connecting to the DB and has the various methods for inserting, selecting etc. I want to test whether an actor was correctly inserted into the database. My Insert method currently looks like this: public void Insert(string firstname, string lastname) { string query = $"INSERT INTO `sakila`.`actor`(`first_name`,`last_name`) VALUES('" + firstname + "','" + lastname + "')"; Console

How to mock a method that returns an int with MOQ

微笑、不失礼 提交于 2019-12-01 21:03:55
I have a class that does some retrieving of contents, and it has a method that requires some inputs (filters) before retrieving it. One of the "input" calls another method, which basically returning an int, how do I mock it using MOQ? Here's an example: namespace MyNamespace { public class ConfigMetaDataColumns : MyModel { public int FieldID { get { return ValueInt("FieldID"); } } public int OrderId { get { return ValueInt("OrderId"); } } public string Label { get { return ValueString("Label"); } } public string FieldName { get { return ValueString("FieldName"); } } public int IsReadOnly { get

asp.net mvc: how to mock Url.Content(“~”)?

六眼飞鱼酱① 提交于 2019-12-01 21:00:37
问题 anybody knows how to mock Url.Content("~") ? (BTW: I'm using Moq) 回答1: You are referring to the Url property in the controllers, I presume, which is of type UrlHelper . The only way we have been able to mock this is to extract an IUrlHelper interface, and create a UrlHelperWrapper class that both implements it and wraps the native UrlHelper type. We then define a new property on our BaseController like so: public new IUrlHelper Url { get { return _urlHelper; } set { _urlHelper = value; } }