moq

ASP.NET MVC unit testing custom AuthorizeAttribute

淺唱寂寞╮ 提交于 2019-12-18 04:05:07
问题 I'm working on an ASP.NET MVC 4 project (.NET framework 4) and I was wondering how to properly unit test a custom AuthorizeAttribute (I use NUnit and Moq). I overrode 2 methods: AuthorizeCore(HttpContextBase httpContext) and HandleUnauthorizedRequest(AuthorizationContext filterContext) . As you can see, these methods expect an HttpContextBase and AuthorizationContext respectively, but I don't know how to Mock these. This is as far as I got: [Test] public void HandleUnauthorizedRequest

Moq It.Is<> not matching

十年热恋 提交于 2019-12-18 03:39:16
问题 This code: hub.MockedUserRepository.Setup(r => r.Update(It.IsAny<ControllUser>())) .Callback((ControllUser usr) => Console.WriteLine("NULL = " + (usr.Zombies[0].ConnectionId == null))) .Verifiable(); Will print NULL = True So i am thinking using this matching will catch it: var zombieDisconnectParameterMatcher = It.Is<ControllUser>(x => x.Zombies[0].ConnectionId == null); hub.MockedUserRepository.Setup(r => r.Update(zombieDisconnectParameterMatcher)) .Callback((ControllUser usr) => Console

How to Moq Entity Framework SqlQuery calls

杀马特。学长 韩版系。学妹 提交于 2019-12-18 03:05:31
问题 I've been able to mock DbSet 's from entity framework with Moq using this link. However, I would now like to know how I could mock the call to SqlQuery. Not sure if this is possible or how as it relies on the mocked db context knowing what "query" is being called. Below is what I am trying to mock. var myObjects = DbContext.Database .SqlQuery<MyObject>("exec [dbo].[my_sproc] {0}", "some_value") .ToList(); I currently haven't tried anything as did not know how to start mocking this example.

is it possible to mock/fake an extension method?

左心房为你撑大大i 提交于 2019-12-17 20:58:13
问题 I'm using a controller extension, and I tried to mock it using FakeItEasy (v 1.7.4) like this: A.CallTo(() => controller.RenderView(A<string>.Ignored,A<object>.Ignored,null)).Returns(""); but I get this error: System.NullReferenceException : Object reference not set to an instance of an object. at System.Object.GetType() at FakeItEasy.Creation.ProxyGeneratorSelector.MethodCanBeInterceptedOnInstance(MethodInfo method, Object callTarget, ref String failReason) at FakeItEasy.Configuration

Mocking framework for asp.net core 5.0

徘徊边缘 提交于 2019-12-17 19:18:51
问题 I recently installed Visual Studio 2015 and started a project with a web site and a asp class library which will contain the unit tests for the web site. I usually use Moq for mocking but I am no stranger to try a different mocking framework. The problem I am having is that I added Moq as a reference to the unit test project and started using it. Everything seems fine at first until I tried to compile. When I compiled I got an error message saying: ASP.NET Core 5.0 error CS0246: The type or

MOQ - Mocking MVC Controller's Response.Cookies.Clear()

流过昼夜 提交于 2019-12-17 16:34:32
问题 I am new to MOQ, but am using it with NUnit for unit testing. I have all parts of my controller mocked, except the following line which throws an 'Object not set to an instance of an object' error message. Response.Cookies.Clear(); I have the following extension method to mock the controller context which works for everything else I have come accross so far (very much thanks to the good people on this forum). public static int SetUpForTest(this System.Web.Mvc.Controller ctl, string username,

How to MOQ an Indexed property

让人想犯罪 __ 提交于 2019-12-17 16:24:10
问题 I am attempting to mock a call to an indexed property. I.e. I would like to moq the following: object result = myDictionaryCollection["SomeKeyValue"]; and also the setter value myDictionaryCollection["SomeKeyValue"] = myNewValue; I am doing this because I need to mock the functionality of a class my app uses. Does anyone know how to do this with MOQ? I've tried variations on the following: Dictionary<string, object> MyContainer = new Dictionary<string, object>(); mock.ExpectGet<object>( p =>

When mocking a class with Moq, how can I CallBase for just specific methods?

偶尔善良 提交于 2019-12-17 16:11:46
问题 I really appreciate Moq's Loose mocking behaviour that returns default values when no expectations are set. It's convenient and saves me code, and it also acts as a safety measure: dependencies won't get unintentionally called during the unit test (as long as they are virtual). However, I'm confused about how to keep these benefits when the method under test happens to be virtual. In this case I do want to call the real code for that one method, while still having the rest of the class

How to Mock the Internal Method of a class?

限于喜欢 提交于 2019-12-17 16:04:24
问题 I have a class which has a internal method and i want to mock the internal method . But i am unable to mock it i.e. it is not calling the mocked function but calling the original function. Is there any way to achieve this ? Edit:Actually i am a novice to the Moq. I have many classes and methods of the classes to test using the Moq. Many classes are internal , many have internal methods, many have not-virtual methods . And can not change the signature on the methods and classes. Can anyone

How to mock Controller.User using moq

穿精又带淫゛_ 提交于 2019-12-17 15:46:31
问题 I have a couple of ActionMethods that queries the Controller.User for its role like this bool isAdmin = User.IsInRole("admin"); acting conveniently on that condition. I'm starting to make tests for these methods with code like this [TestMethod] public void HomeController_Index_Should_Return_Non_Null_ViewPage() { HomeController controller = new HomePostController(); ActionResult index = controller.Index(); Assert.IsNotNull(index); } and that Test Fails because Controller.User is not set. Any