moq

How can I decouple my application from my membership service?

限于喜欢 提交于 2019-12-08 03:36:47
问题 I'm working on an ASP.NET MVC 4 project that uses Castle Windsor. One of the controllers has a dependency on MembershipService: public class FooController : Controller { private readonly MembershipService MembershipService; public FooController( MembershipService membershipService ) { MembershipService = membershipService; } [Authorize( Roles = "Administrator" )] public ActionResult DoSomething() { var user = MembershipService.GetUser( User.Identity.Name ); // etc... } } When I started

Moq unit test with It.IsAny<DateTime>() fails

北慕城南 提交于 2019-12-08 02:11:54
问题 I'm using Moq to write the unit tests for a project, and one of the tests is failing when I try to verify that a DateTime property is assigned a value. Here's my Verify (which fails): _mockTaskContext.Verify(context => context.TaskQueue.AddObject(It.Is<TaskQueueItem>( task_queue => task_queue.TaskCode == (int)TaskCode.MyTask && task_queue.ClientID == ExpectedClientID && task_queue.JobNumber == It.IsAny<int>() && task_queue.Requester == String.Empty && task_queue.JobStatus == (int)JobStatus

ASP.NET MVC test for action filters

安稳与你 提交于 2019-12-08 01:35:47
问题 How can i write test case for action filters? I am using forms authentication. I have base controller decorated with RequiresAuthentication action filter. When i execute the controller's test case, i am not getting the loggedin user's data from the cookies. I am using Moq; does it provide a way to achieve my goal? 回答1: This blog post from Scott Hanselmann covers MvcMockHelpers including FakeHttpContext for different mocking frameworks among others also Moq: using System; using System.Web;

How can I stub an interface method using Moq

时间秒杀一切 提交于 2019-12-08 01:18:33
问题 Is there a way to stub out a method using Moq? I saw quite a few questions with similar titles, but none that actually answered my question. Here was a unit testing example I was given and I found it very difficult to test using Moq. What I would like to do is unit test the EmailTasks.UserIsValidForNotice() method: public class User { public DateTime JoinDate { get; set; } public bool Active { get; set; } } public class EmailTasks { IUserRepository repo; public EmailTasks(IUserRepository repo

Need Help understanding this code

百般思念 提交于 2019-12-08 00:04:16
问题 I am trying to learn unit testing. I am trying to unit test some Memembership stuff I am making in asp.net mvc 1.0. I been following a book on MVC and I am confused about some stuff that hopefully someone can clear up for me. I am using Nunit and Moq for my frameworks. Question 1: public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider provider) { FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); Provider = provider ?? Membership.Provider; } I am kinda

What is the difference between NET40-RequiresCastle and NET40 version of Moq?

吃可爱长大的小学妹 提交于 2019-12-07 22:41:05
问题 In the distribution of moq, there are two versions, NET40-RequiresCastle and NET40. The NET40-RequiresCastle seems to have much smaller size than the NET40, but requires Castle during runtime. Is the difference only the fact that Castle is embedded inside the NET40 version, or is there going to be difference in performance, usage, etc? Is there a reason to use NET40-RequiresCastle instead of NET40? 回答1: MoQ uses Castle DynamicProxy for proxy generation. In order to simplify the deploy that

Issue with mocking IOrganizationService.Execute in CRM 2011 plugin

家住魔仙堡 提交于 2019-12-07 15:33:22
问题 I am still new to mocking and I am having trouble with this code: //create the request SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest { Target = email, TemplateId = new Guid("07B94C1D-C85F-492F-B120-F0A743C540E6"), RegardingId = toParty[0].PartyId.Id, RegardingType = toParty[0].PartyId.LogicalName }; //retrieve response SendEmailFromTemplateResponse emailUsingTemplateResponse = (SendEmailFromTemplateResponse)service.Execute(emailUsingTemplateReq); var

Checking that CancellationTokenSource.Cancel() was invoked with Moq

こ雲淡風輕ζ 提交于 2019-12-07 14:44:03
问题 I have a conditional statement which should looks as follows: //... if(_view.VerifyData != true) { //... } else { _view.PermanentCancellation.Cancel(); } where PermanentCancellation is of type CancellationTokenSource. Im wondering how i should set this up in my mock of _view. All attempts thus far have failed :( and i cant find an example on google. Any pointers would be appreciated. 回答1: Because CancellationTokenSource.Cancel is not virtual you cannot mock it with moq. You have two options:

Why do I get a NullReferenceException when testing this async method with MSpec/Moq?

怎甘沉沦 提交于 2019-12-07 13:58:13
问题 I want test if the correct type is returned from an async method. This method uses another async method in a dependency class. The dependency class implements this interface: Task<string> DownloadStringAsync(string url); The method I want to test is this: public async Task<T> GetData<T>(string url) where T : class , new() { var jsonData = await _webClientWrapper.DownloadStringAsync(url); if (string.IsNullOrEmpty(jsonData)) return new T(); try { return await JsonConvert.DeserializeObjectAsync

Setting properties on a mocked HttpContextBase

▼魔方 西西 提交于 2019-12-07 13:27:11
问题 I'm working on an ASP.NET MVC application, and am trying to write some unit tests against controller actions, some of which manipulate properties on the HttpContext, such as Session, Request.Cookies, Response.Cookies, etc. I'm having some trouble figuring out how to "Arrange, Act, Assert"...I can see Arrange and Assert...but I'm having trouble figuring out how to "Act" on properties of a mocked HttpContextBase when all of its properties only have getters. I can't set anything on my mocked