moq

FormsAuthentication.SetAuthCookie mocking using Moq

余生长醉 提交于 2019-12-05 03:23:40
Hi i'm doing some unit test on my ASP.Net MVC2 project. I'm using Moq framework. In my LogOnController, [HttpPost] public ActionResult LogOn(LogOnModel model, string returnUrl = "") { FormsAuthenticationService FormsService = new FormsAuthenticationService(); FormsService.SignIn(model.UserName, model.RememberMe); } In FormAuthenticationService class, public class FormsAuthenticationService : IFormsAuthenticationService { public virtual void SignIn(string userName, bool createPersistentCookie) { if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.",

Can this be mocked with Moq?

久未见 提交于 2019-12-05 03:16:44
问题 I am working on mocking some external dependencies and am having trouble with one 3rd party class that takes in it's constructor an instance of another 3rd party class. Hopefully the SO community can give me some direction. I want to create a mock instance of SomeRelatedLibraryClass that takes in it's constructor a mock instance of SomeLibraryClass . How can I mock SomeRelatedLibraryClass this way? The repo code... Here is a Main method that I am using in my test console application. public

Can I use moq's InSequence() with MockBehavior.Loose?

偶尔善良 提交于 2019-12-05 02:31:26
I was trying to perform subsequent calls verification and I found that moq supports the InSequence() method for this, like: MockSequence s = new MockSequence(); validator.InSequence(s).Setup(m => m.IsValid(It.IsAny<Frame>())).Returns(true); encryptor.InSequence(s).Setup(m=>m.Encrypt(It.IsAny<Frame>())); socket.InSequence(s).Setup(m => m.Send(It.IsAny<Frame>())); compressor.InSequence(s).Setup(m => m.Compress(It.IsAny<Frame>())); However, this seems to be working only when I specify mock behavior as "strict", which forbids me calling additional mehods on mocked objects. I'd like, however, to be

Getting arguments passed to a FakeItEasy-mock without using magic strings?

和自甴很熟 提交于 2019-12-05 02:24:10
I have been using Moq for my mocking needs the last years, but after looking at FakeItEasy i wanted to give it a try. I often want to test that a method have been called with the correct parameters, but i found no satisfactory way to do this with FakeItEasy. I have the following code to test: public class WizardStateEngine : IWizardStateEngine { private readonly IWorkflowInvoker _workflowInvoker; private List<CustomBookmark> _history; public WizardStateEngine(IWorkflowInvoker workflowInvoker) { _workflowInvoker = workflowInvoker; } public void Initialize(List<CustomBookmark> history) {

Mocking indexed property

安稳与你 提交于 2019-12-05 01:46:57
I am writing unit tests using Moq. I have created a mock object. Now when i try to mock its property i am getting error "An expression tree may not contain an indexed property" here is my code. public Node GetNode(IMyInterface interface, string itemName) { return interface.Items[itemName]; } Here is the unit test var expected = new Node(); var itemName = "TestName"; var mock = new Mock<IMyInterface>(); mock.Setup(f => f.Items[itemName]).Returns(expected); var target = new MyClass(); var actual = target.GetNode(mock.Object, itemName); Assert.AreEqual(expected, actual); This line is giving me

TypeMock VS JustMock (VS RhinoMock,Moq…): current situation in 2011? [closed]

末鹿安然 提交于 2019-12-05 01:39:01
I've started TDD some weeks ago. I have to do Unit Tests on a C# code full of non-virtual methods and there is no much interface either. Therefore, after I've been studying RhinoMock and Moq, a proxy solution wasn't enough: what I need is an isolation thanks to a profiler. From what I read, I have 3 choices: TypeMock Isolator, very powerful but also very expensive http://www.typemock.com/ JustMock of Telerik, the alternative to TypeMock which aims to be TypeMock, but the beta was full of bugs http://www.telerik.com/products/mocking.aspx Moles from Microsoft Pex, the only free solution for

Using Moq to Stub an interface method [duplicate]

感情迁移 提交于 2019-12-05 01:26:04
Possible Duplicate: How to mock a method that returns an int with MOQ Here's my interface: public interface ICalenderService { DateTime Adjust(DateTime dateToAdjust, BusinessDayConvention convention, List<HolidayCity> holidayCities); } I've done some research and it seems you can mock this real easily, but I want to stub this out using Moq so that I can pass the stub into my other class constuctors and have the stub return whatever DateTime I want for its Adjust method. What's the easiest way to do this? Edit: I know I can create my own stub in my project, but I'd like to write less code, and

Mocking HttpRequest in ASP.NET 4.0

泪湿孤枕 提交于 2019-12-05 00:53:25
问题 I've seen a lot of similar threads but none that actually address my particular situation. I'm writing unit tests in ASP.NET 4.0 web application (ASP.NET Forms, not MVC). There are several spots in the code where I call the ServerVariables collection to call variables like REMOTE_ADDR . Since my unit tests do not actually initiate HttpRequests when executing my code, things like ServerVariables are Null and therefore error when I try to call HttpContext.Current.Request.ServerVariables("REMOTE

xUnit and Moq do not support async - await keywords

女生的网名这么多〃 提交于 2019-12-05 00:06:00
I am trying to discover how to apply the async and await keywords to my xUnit tests. I am using xUnit 1.9 and Async CTP 1.3. Here is my test case I have an interface which specifies one asynchronous method call public interface IDoStuffAsync { Task AnAsyncMethod(string value); } I have a class which consumes the interface and calls the async method public class UseAnAsyncThing { private readonly IDoStuffAsync _doStuffAsync; public UseAnAsyncThing(IDoStuffAsync doStuffAsync) { _doStuffAsync = doStuffAsync; } public async Task DoThatAsyncOperation(string theValue) { await _doStuffAsync

How to modify an invocation parameter of a mocked method with Moq?

扶醉桌前 提交于 2019-12-05 00:01:21
Is it possible to modify an invocation parameter of a mocked method? In particular I'm looking to change buffer in the following example to a pre-populated byte array. Example: int MockedClass.Read(byte[] buffer, int offset, int count) Explanation: Calling Read loads count bytes reading from offset into the supplied byte array buffer . Now I would like to have buffer populated after the call to Read has been made in my application code. Is that possible? If yes, how would I go about successive calls to Read ? I would like successive calls to return a different buffer each time if possible.