moq

Test calls to private methods with moq

隐身守侯 提交于 2020-01-01 09:19:15
问题 I have the following method I need to test with Moq. The problem is that each method called in the switch statement is private, including the PublishMessage at the end. But this method (ProcessMessage) is public. How can I test this so that I can ensure the calls are made depending on the parameter? Note that I'm not testing the private methods, I just want to test the "calls". I'd like to mock these private methods and check if they are called using Setup, but Moq does not support mocking

Moq testing void method

一笑奈何 提交于 2020-01-01 08:43:21
问题 Hi I am new to Moq testing and having hard time to do a simple assertion. I am using an interface public interface IAdd { void add(int a, int b); } Moq for the IAdd interface is: Mock<IAdd> mockadd = new Mock<IAdd>(); mockadd.Setup(x => x.add(It.IsAny<int>(), It.IsAny<int>()).callback((int a, int b) => { a+b;}); IAdd testing = mockadd.Object; Since the add method is void, it doesn't return any value to Assert with. How can I assert this setup? 回答1: Why mocking is used? It used for verifying

Moq testing void method

时间秒杀一切 提交于 2020-01-01 08:43:08
问题 Hi I am new to Moq testing and having hard time to do a simple assertion. I am using an interface public interface IAdd { void add(int a, int b); } Moq for the IAdd interface is: Mock<IAdd> mockadd = new Mock<IAdd>(); mockadd.Setup(x => x.add(It.IsAny<int>(), It.IsAny<int>()).callback((int a, int b) => { a+b;}); IAdd testing = mockadd.Object; Since the add method is void, it doesn't return any value to Assert with. How can I assert this setup? 回答1: Why mocking is used? It used for verifying

Does Moq.Mock.Verify() compare parameters using identity or .Equals()?

ぐ巨炮叔叔 提交于 2020-01-01 08:32:30
问题 In a command like var mockObj = new Mock<MyObject>() var anotherObj = Utilities.DoStuff(); // some tests... mockObj.Verify(foo => foo.someMethod(anotherObj)); Does Moq use comparison by identity or by using .Equals() to determine whether someMethod() was ever called with anotherObj as a parameter? In other words, does the object I indicate as a parameter for foo.someMethod() have to be the exact same object that someMethod() was called with earlier for the verification to pass, or does it

Does Moq.Mock.Verify() compare parameters using identity or .Equals()?

社会主义新天地 提交于 2020-01-01 08:31:16
问题 In a command like var mockObj = new Mock<MyObject>() var anotherObj = Utilities.DoStuff(); // some tests... mockObj.Verify(foo => foo.someMethod(anotherObj)); Does Moq use comparison by identity or by using .Equals() to determine whether someMethod() was ever called with anotherObj as a parameter? In other words, does the object I indicate as a parameter for foo.someMethod() have to be the exact same object that someMethod() was called with earlier for the verification to pass, or does it

Instantiate new System.Web.Http.OData.Query.ODataQueryOptions in nunit test of ASP.NET Web API controller

风流意气都作罢 提交于 2020-01-01 08:14:34
问题 I have an ASP.NET MVC4 Web API project with an ApiController-inheriting controller that accepts an ODataQueryOptions parameter as one of its inputs. I am using NUnit and Moq to test the project, which allow me to setup canned responses from the relevant repository methods used by the ApiController. This works, as in: [TestFixture] public class ProjectControllerTests { [Test] public async Task GetById() { var repo = new Mock<IManagementQuery>(); repo.Setup(a => a.GetProjectById(2)).Returns

Proper way of testing ASP.NET Core IMemoryCache

十年热恋 提交于 2020-01-01 08:06:12
问题 I'm writing a simple test case that tests that my controller calls the cache before calling my service. I'm using xUnit and Moq for the task. I'm facing an issue because GetOrCreateAsync<T> is an extension method, and those can't be mocked by the framework. I relied on internal details to figure out I can mock TryGetValue instead and get away with my test (see https://github.com/aspnet/Caching/blob/c432e5827e4505c05ac7ad8ef1e3bc6bf784520b/src/Microsoft.Extensions.Caching.Abstractions

Invoking Actions from Moq

北城余情 提交于 2020-01-01 06:43:13
问题 I've got a service with a method that takes two Action s, one for success and one for failure. Each Action takes a Result parameter that contains additional information... void AuthoriseUser(AuthDetails loginDetails, Action<AuthResult> onSuccess, Action<AuthResult> onFailure); I'm writing a unit test for a class that is dependant on that service, and I want to test that this class does the correct things in the onSuccess(...) and onFailure(...) callbacks. These are either private or anonymous

Mocking a HttpContext Response.Output with Moq

僤鯓⒐⒋嵵緔 提交于 2019-12-31 22:39:31
问题 I've been using the MvcMockHelpers class found at Hanselman's blog for passing in a mocked HttpContext. We extended it somewhat to add some authentication data we needed and for the most part this has been great. The issue we are having is that the context we are giving to the controller has a null value in the HttpContext.Response.Output, which is causing some exceptions to be thrown. I'm not sure what to add to get this working. Here is the existing FakeHttpConext() method: public static

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

≯℡__Kan透↙ 提交于 2019-12-31 02:09:41
问题 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