mocking

testing objects using DAO

Continuing another but similar question about testing (see here). I'll use a similare example (pseudocode) class LinkDisplayer method constructor(LinkStorage) method displayLatestLinksByCategory(number_of_them) class LinkStorage method saveLink(Link) method retrieveLatestLinksByCategory(category, number_of_them) class ...

Preferred unit testing isolation framework for .net

I have used the RhinoMocks product for quite some time and have been quite happy with the product - never had a reason to look elsewhere really. I was recently asked by the good folks at TypeMock to give their product a whirl and was wondering what other developers opinions are about Typemock Isolator. Are there any other isolation/moc...

Should I change code to make it more testable?

Hi, I often find myself changing my code to make it more testable, I always wonder whether this is a good idea or not. Some of the things I find myself doing are: Adding setters just so I can set an internal object to a mock. Adding getters for internal maps/lists so I can check the internal state of the object has changed after perf...

Moq Match and Verify Array / IEnumerable parameters in method setup

I'm having problems verifying Ienumerable / Array type parameters when setting up expectation for methods call on my mock objects. I think since it's matching different references it doesn't consider it a match. I just want it to match the contents of the array, sometimes I don't even care about the order. mockDataWriter.Setup(m => m....

When to use stubs and mocks?

I've this confusion all the time. If I write a code which uses fake code to assert some operation, how do i trust my real implementation when it is started really using the real objects instead of fake ones. For example, I've this code -- [Test] public void CanCreateContactsWithData() { using(ISession session = fact...

How do i mock a method that accepts a handle as an argument in OCMock?

I'm trying to mock a method that has the equivalent of the following signature: - (NSDictionary *) uploadValues:(BOOL)doSomething error:(NSError **)error I want it to return a small dictionary so that my test can make sure the code uses the dictionary properly. however, no matter what i do OCMock always returns nil from the method, r...

Conversation mock data - where to get or how to generate

I'm writing a special sort of chat/forum software and need a source of mock conversations to use in screenshots, demos and tests. They should contain no real names or other potentially harmful information, should be 100% correct English and should make at least some sense in relation to each other. Example: Message #1 Subject: Hell...

How do I mock an open used in a with statement (using the Mock framework in Python)?

How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by Michael Foord's Mock framework): def testme(filepath): with open(filepath, 'r') as f: return f.read() ...

State/Interaction testing and confusion on mixing (or abusing) them

Hello, I think understand the definition of State / Interaction based testing (read the Fowler thing, etc). I found that I started state based but have been doing more interaction based and I'm getting a bit confused on how to test certain things. I have a controller in MVC and an action calls a service to deny a package: public Acti...

How do you test an extension method on an Interface which calls another method in that Interface with a mock created by Rhino Mocks?

I'm testing an extension method on an interface 'ISomeInterface'. The extension method actually calls to another method in the interface. How do I set up a mock and the appropriate expectations (i want to set the expectation that 'SomeMethod', which is an defined method in the interface with a different signature then this extension met...

"Newing up" Interfaces

A couple of days ago, I saw the CoClassAttribute being used in a way I haven't imagined before. [ComImport, CoClass(typeof(Foo)), Guid("787C1303-AE31-47a2-8E89-07C7257B1C43")] interface IFoo { void Bar(); } class Foo : IFoo { public void Bar() { Console.WriteLine("Oh retado!"); } } being used as: class CoClass...

How can I access private class members in Java?

I have data model classes that contain private fields which are meant to be read-only (via a getter function). These fields are set by my JPA persistence provider (eclipselink) during normal operation, using the contents of the database. For unit tests, I want to set them to fake values from a mockup of the persistence layer. How can I d...

In Java, can I create an instance of an anonymous subclass from a Class object?

I have a factory method that creates objects to be used in unit tests. These objects all derive from the same base class: public static <T extends BaseEntity> T modMake(Class<T> clazz) { try { return clazz.newInstance(); } catch (InstantiationException e) { // Should never happen throw new AssertionError(...

Is it possible to Mock out time in a unit test?

Following on from this question...I'm trying to unit test the following scenario: I have a class that allows one to call a method to perform some action and if it fails wait a second and recall that method. Say I wanted to call a method DoSomething()...but in the event of an exception being thrown by DoSomething() I want to be able to...

rspec mock question

I am trying to Mock an object that is being passed into another object, and am having no success. Can someone show me what I am doing wrong? class Fetcher def download return 3 end end class Reports def initialize(fetcher) @fetcher = fetcher end def status @fetcher.download end end describe Reports do before...

How to organize a Data Base Access layer?

I am using SqlAlchemy, a python ORM library. And I used to access database directly from business layer directly by calling SqlAlchemy API. But then I found that would cause too much time to run all my test cases and now I think maybe I should create a DB access layer, so I can use mock objects during test instead of access database di...

What .NET mocking frameworks allow me to specify ANYTHING as an expected argument?

Hi, I am used to using JMock in Java which allows you to specify things such as any(String.class) as an expected argument - which .NET frameworks offer similar functionality? ...

Is there a way to specify ANYTHING as an argument to NUnit Mocks Expect call?

hi, I'm using NUnit mocks and would like to specify that I expect a call but without saying what the arguments will be for example: mock.ExpectAndReturn("Equals", true, ANY_ARGUMENT); Obviously filling in the correct syntax instead of ANY_ARGUMENT. Is there a way to do this? If I specify no arguments - NUnit fails the test because ...

Rhino Mocks, Interfaces and properties

I have a class that has a property that I need to stub. I can't pass it as part of the constructor because the object constructing it does not know the parameters of the constructor. When running unit tests, I want to be able to have the property be created as a stub. This is what I have tried, but it does not work: private DeviceMed...

Yet another question on mocking....

First let me state that, despite being a fairly new practitioner of TDD, I'm pretty much sold on its benefits. I feel like I've progressed enough to consider using mocks and have hit a real brick wall when it comes to understanding where mocks fit in with OOP. I've read as many relevant posts/articles on the subject as I could find (Fo...