mocking

Mocking System.Drawing.Image with Moq

How would I go about mocking an Image with Moq? It has no constructors (is always generated from factory methods). Basically I want to do something like this... var image = new Mock<Image>(); image.Setup(i=>i.Save(someStream,ImageFormat.Jpeg); var testableObject = new ObjectUnderTest(image.Object); testableObject.MethodBeingTested();...

How to mock a web service

Do I have to rewrite my code to do this into an interface? Or is there an easier way? I am using Moq ...

SimpleTest Mock objects: clearing expectations

The short question: Is there a way to reset a Mock object in SimpleTest, removing all expectations? The longer explanation: I have a class that I'm testing using SimpleTest and am having some problem with the Mock objects it is using. The class is a Logger, and inside the logger are a number of Writer objects (FileWriter, EmailWriter,...

Design for using a mock connection

I have a simple application with a UI with a backend connection, from which i get the data. Now, during development, I wouldn't like to connect to the backend as it slows me down. So, i have some locally stored data and i 'mock' the connection to return the locally stored data instead of making a server call. Now, this is not for writin...

Using Moq: mock object throwing 'TargetParameterCountException'

I am new to Moq, so hopefully I am just missing something here. For some reason I am getting a TargetParameterCountException. Can you see what I am doing wrong? Any questions? Please ask. :) Here's my code: [Test] public void HasStudentTest_SaveToRepository_Then_HasStudentReturnsTrue() { var fakeStudents = new List<Stude...

Mock system call in ruby

Know of a way to mock %[]? I'm writing tests for code that makes a few system calls, for example: def log(file) %x[git log #{file}] end and would like to avoid actually executing system calls while testing this method. Ideally I'd like to mock %x[..] and assert that the correct shell command is passed to it. ...

Creating a mock DAO object that returns my Entity class for testing?

I'm looking for a little jump start with mock objects. I have a class called ProjectDAO that has a method called 'GetById' that will load and return a 'Project' object after tripping to the database. GetById takes a single string argument. Public Interface IEntityDAO Function GetByID(ByVal projectNumber As String) As Project End In...

Mocking using boost::shared_ptr and AMOP

Hi, I'm trying to write mocks using amop. I'm using Visual Studio 2008. I have this interface class: struct Interface { virtual void Activate() = 0; }; and this other class which receives pointers to this Interface, like this: struct UserOfInterface { void execute(Interface* iface) { iface->Activate(); } }; So I try...

Mocking java object for unit test

Hi, I am looking for a good unit test framework that I can use to mock private methods that can run under JDK 1.4.2. Cheers, ...

TDD, Mocking, dependency injection and the DRY principle

I've got a controller class which accpets multiple parameters in the ctor which gets injected at runtime. Example: public ProductController(IProductRepositort productRepository, IShippingService shippingService, IEmailProvider emailProvider) { ... } I am finding that the Test methods are getting huge. I am setting u...

Mocking an arry of type with Rhino Mock

I am having issues mocking an array with Rhino Mock, any direction would be great. namespace Checks_Rhino_Mocks { public class Check { public Header header; public Detail[] details; } public class Header { public string Number; public decimal Amount; } public class Detail ...

Mock object and Spring annotations

I am using Spring annotations in my code to do the DI. So lets say I have a class class1 that depends on another class class2, I define class1 as below: @Component public class class1 { @Resource private interface2 object2; } class2 is an implementation of interface2. Now lets say I want to mock class2 and pass it to class1, I don...

Moq - How to verify that a property value is set via the setter

Consider this class: public class Cotent { public virtual bool IsCheckedOut {get; private set;} public virtual void CheckOut() { IsCheckedOut = true; } public virtual void CheckIn() { //Do Nothing for now as demonstrating false positive test. } } The Checkin method is intentionally empty. Now i have ...

Moq Mocking and tracking Session values

I'm having problems returning a Session value set from mocking using Moq. Using the following public class TestHelpers { public long sessionValue = -1; public HttpContextBase FakeHttpContext() { var httpContext = new Mock<HttpContextBase>(); var session = new Mock<HttpSessionStateBase>(); httpContext.Setup(x => x.Session).Retu...

Unit Test - Check a method call with an object that doesn't override equals

Hi All, This is a general question on how to unit-test a Java Class using mock objects. I can summarize my problem with this example. Let's say I've an Interface called MyInterface.java and a "TwoString" Object that doesn't override equals() "TwoString.java" private String string1; private String string2; public TwoString(...

Unit Testing HtmlHelper that Needs to Access Data Layer

I am trying to create a unit test for an HtmlHelper that accesses my data layer to get a string. I have looked at a lot of posts around this and I am probably missing something. The problem that I am having is how do I mock the access to my data layer? I usually do my dependency ijection through the constructor, but I cannot here beca...

What is a simple example of using Seam's MockExternalContext to unit test?

I'm trying to unit test my seam components, and so far I've been doing pretty well with the EasyMock that my team has been using. However, I have run into a wall with javax.faces.context.ExternalContext. Since it's not an interface, I can't run EasyMock.createMock() on it. Seam has a MockExternalContext, but I have no idea how to use ...

Is there any free mocking framework that can mock static methods and sealed classes?

Typemock can do this but it is $799USD and that is a lot to pay for two features. Please no conversations about avoiding using static and sealed things or encapsulating them. When using a large 3rd party API it is not possible and/or practical. Thanks ...

How do I set the expectation of an event when calling a method?

I've implemented an ITimer interface because I want to write some tests around a class I'm building that utilizes the System.Timers.Timer class. So the sequence goes when I call Timer.Start() some time later I expect the Elapsed event to occur. However, for my test I want to mock out this behavior, because I don't want to wait a certai...

Unittesting Corba in Python.

Hi there, I am interested in your opinions on unittesting code that uses Corba to communicate with a server. Would you mock the Corba objects? In Python that's sort of a pain in the ass because all the methods of Corba objects are loaded dynamically. So you're basically stuck with "mock anything". Thanks! Note: I believe I have not m...