moq

Reflecting on a Moq Object produces 2 additional properties

I've got a couple of methods that use reflection to transform from one object type to another. I'm in the process of testing the transformation methods via Moq and have stumbled upon a behavior I don't know how to handle. When I reflect across a Moq object to obtain PropertyInfo's, I get two additional objects. Moq.Mock``1[Namespac...

Mocking VB.NET Methods With Moq

I am trying to unit test a controller action that uses the membership provider to update user details. I am using Moq which so far has been easy to use. The problem is I can't seem to get it to mock calls to methods that don't return anything. <TestMethod()> _ Public Sub Can_Update_User() ' Arrange _membershipService.Setup(Functio...

Moq Invocation was not perfomed on the mock.Can you help explain.

Hi all Beginner in Moq. Trying to understand the use of verifySet etc... but unless I do a workaround I cannot get it to work. public interface IProduct { int Id { get; set; } string Name { get; set; } } public void Can_do_something() { var newProduct = new Mock<IProduct>(); newProduct.SetupGet(p => p.Id).Returns(1);...

Unit testing an HttpApplication

I have a class derived from HttpApplication that adds some extra features. I'm to the point where I need to unit test these features, which means I have to be able to create a new instance of the HttpApplication, fake a request, and retrieve the response object. How exactly do I go about unit testing an HttpApplication object? I'm using...

How do I mock HttpResponseBase.End()?

I'm using Moq to create a mock object of HttpResponseBase. I need to be able to test that HttpResponseBase.End() was called in my library. To do this, I specify some text before the call and some text after. Then I check that only the text before the call to End() is present in HttpResponseBase.Output. The problem is, I can't figure out...

Why does my Moq IEventAggregator verification fail?

I use Composite WPF(Prism) and I am trying to unit test that my Controller does in fact subscribe to a Composite Event. My subscription code looks as follows... //Init Events. this.eventAggregator.GetEvent<PlantTreeNodeSelectedEvent>().Subscribe( ShowNodeDetails, ThreadOption.UIThread); My unit testing code looks as follows (I us...

Moq custom IIdentity

Hi, I created a custom RoleProvider (standard webforms, no mvc) and I would like to test it. The provider itself integrates with a custom implementation of IIdentity (with some added properties). I have this at the moment: var user = new Mock<IPrincipal>(); var identity = new Mock<CustomIdentity>(); user.Setup(ctx => ctx.Identity).R...

Unit testing custom RoleProvider with Moq?

I created a custom RoleProvider in a custom library. I would like to unit test it. Via Moq I created a fake HttpContextBase. How to pass this to the to be tested RoleProvider? The Identity is a custom test implementation class. This works fine. I only don't know how to pass in the fake context in my provider. This is not an MVC applicat...

Strange behavior in the "Verify" method in Moq

In the following code, Test1 succeeds but Test2 fails: protected Mock<IMyInterface> MyMock { get; set; } [SetUp] public virtual void Initialize() { MyMock = new Mock<IMyInterface>(); } [Test] void Test1() { // ... code that causes IMyIntervace.myMethod to be called once MyMock.Verify(x=> x.myMethod(), Times.Once()); } [...

Is it possible to Mock a Database Transaction parameter?

I'm trying to unit-test my implementation of an interface, and I'm having a little difficulty successfully mocking out a SqlTransaction parameter to one of the interface methods. Here's what the interface, and method under test that I'm interested in look like.. public class MyInterface { void MyMethod(SqlTransaction SharedTransact...

"Short circuiting" void methods with Moq?

Hi, my team has made the decision recently to use Moq as our mocking framework for its tremendous flexibility and highly readable syntax. As we're new to it, I'm stumbling on what appears to be simple questions--searches (here, Google, etc.) find plenty of discussions on other nuances of Moq, but not necessarily what I'm after, and the ...

Moq SetupProperty not working

Can someone tell me whey the following code blows when the moq.SetupProperty fails in the code below: [TestMethod] public void SimulatorService_Returns_HighScores() { IScoreService scoreService = new ScoreService(MockScoreRepository.GetMockScoreRepository()); Assert.IsNotNull(scoreService); var highScores = scoreService.GetH...

How to mock the controller context with moq

I am trying out the MOQ framework and up now I have hit a barrier. The following unit test fails because the actual value of the ViewName property is an empty string. Could anyone point me in the right direction please as to why this is not passing the test? [TestMethod] public void Can_Navigate_To_About_Page() { var request = new...

Should I use mocking in production code?

I have a situation where I need to mock some code in production. This is for making one part of code, work in half functionality. I have to choose to write an empty classes (to implement the interface) , or use a mocking system like moq. So the question is, do the mocking systems hit the performance, or break some readability of th...

Moq: unit testing a method relying on HttpContext

Consider a method in a .NET assembly: public static string GetSecurityContextUserName() { //extract the username from request string sUser = HttpContext.Current.User.Identity.Name; //everything after the domain sUser = sUser.Substring(sUser.IndexOf("\\") + 1).ToLower(); return sUser; } I'd l...

The right way to mock (with moq) methods that return mocked objects?

Which of these is correct? var mockLogger = new Mock<EntLibLogger>(); mockLogger.Setup(i => i.CreateTracer(It.IsAny<string>())) .Returns((string operationName) => { var mockTracer = new Mock<EntLibTracer>(operationName); mockTracer.Setup(i => i.IsTracingEnabled()) .Returns(true); ...

TargetParameterCountException in Moq-based unit-test

Hi, We have repositories which have a "Save" method. They also throw a "Created" event whenever an entity is saved. We have been trying to use Moq to mock out the repository as such.... var IRepository = new Mock<IRepository>(); Request request = new Request(); IRepository.Setup(a => a.Save(request)).Raises(a => a.Created += null, Req...

Expectation on Mock Object doesn't seem to be met (Moq)

I'm experiencing some odd behavior in Moq - despite the fact that I setup a mock object to act a certain way, and then call the method in the exact same way in the object I'm testing, it reacts as if the method was never called. I have the following controller action that I'm trying to test: public ActionResult Search(string query, boo...

Can this be mocked with Moq?

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 Som...

Unit testing of extremely trivial methods (yes or no)

Suppose you have a method: public void Save(Entity data) { this.repositoryIocInstance.EntitySave(data); } Would you write a unit test at all? public void TestSave() { // arrange Mock<EntityRepository> repo = new Mock<EntityRepository>(); repo.Setup(m => m.EntitySave(It.IsAny<Entity>()); // act MyClass c = new...