mocking

Should you only mock types you own?

I read through this blog entry by Mark Needham and would like to know if this is best practice or not? Please note that he is not against mocking, but against mocking directly - he does say that writing a wrapper and mocking that is fine. ...

"Self-mocking" using Rhino Mocks

I have a situation I've run into several times but have never found a good answer for. Suppose I have a class like the following, where one method calls another in the same class: public class Foo { public int Bar() { if (Baz()) { return 1; } else { return 2; ...

Training Videos on Mocking

Can anyone point out any useful training videos on introduction to Mocking? Thanks ...

Testing the internals of a method with RhinoMock or Moq

Quite new to this mocking thing, I have a couple of questions. Correct me if I'm wrong: Mocking does not initialize the real method i.e. Mocking will not actually call the constructor of your class. Instead it does something like look at the signature of the class and create an object with that signature but with none of the methods fun...

Mocking - setting a property before calling the constructor

In RhinoMocks or Moq, can properties on an object be set before the constructor is called? I'm trying to test a method. The class containing the method has some code in it's constructor which depends on some members being set, unfortunately there's no params in the constructor to set them so I must set them through a property. Is ther...

Using Groovy MetaClass to overwrite Methods

I have a POJO that uses a service to do something: public class PlainOldJavaObject { private IService service; public String publicMethod(String x) { return doCallService(x); } public String doCallService(String x) { if(service == null) { throw new RuntimeException("Service must not be null...

How to stub abstract collection with Moq?

Let's say I have this class: public abstract class CustomerCollectionBase : Collection<Customer>{} One of my classes under test accepts CustomerCollectionBase instance (it will be some subclass). In the method under test, this collection is enumerated via for loop and results are examined and processed. like follows: for(int i=0;i<_c...

Why use a mocking framework instead of hand-rolling our mocks?

Currently I'm reading a book (Pro ASP.Net Framework). In this book, the author suggests to use a Moq framework to help to do TDD. [Test] public void List_Presents_Correct_Page_Of_Products() { IProductsRepository repository = MockProductsRepository( new Product { Name = "P1" }, new Product { Name = "P2" }, new Produc...

How to verify method arguments after method call?

Say I have a method A.Do(Arg arg) which assigns some properties of arg (class Arg), let say it sets arg.Prop1 = "done". And I'm testing a void method B.Do(void): public class B { public void Do() { var arg = InitArg(); A.Do(arg) ... } } and I've mocked class A as new Mock< A>() with CodeBase=true. So how do I verify...

Unit Test Service which has WCF Call (MSUnit + Moq)

Hi there, I am new to Mocking and somewhat familiar with unit testing and finally have decided to bite the bullet on a new project starting up front with a strict TDD approach. However I have one service class and method I need to retrospectively add tests to as it has been promoted from a prototype. I do not know where to start thoug...

when unit testing an asp.net controller, where do you mock the httprequestbase?

when unit testing a asp.net controller, don't you have to somehow mock the httpcontextbase? All my controllers inherit from a custom controller class that I wrote (it just adds some common properties to the original controller class). So its like: public class MyController : Controller { protected override void OnActionExecuting(Sy...

Using callback triggers with RhinoMocks

I'm writing unit tests using RhinoMocks for mocking, and now I'm in need of some new functionality which I haven't used before. I'd like to call a function, which again will call an async function. To simulate that the async function finishes and triggers the given callback with the result from execution I assume I can use the Callback...

Perl: mocking -d -f and friends. How to put them into CORE::GLOBAL

The CORE documentation has shown me how to merrily mock various built Perl functions. However, I'm not really sure how to replace '-d' &c. with my methods. So this is really just a question on how do i replace a function with a dash in it in CORE::GLOBAL. A manual reference would be nice. package Testing::MockDir; use strict; use warn...

"Forging" (= mocking) an AMFPHP remoting request

I am using AMFPHP with great success to link my database with my Flex application. However I want to be able to test the remoting requests outside of flash, by typing something like: http://localhost/amfphp/gateway.php?%5BWHAT DO I PUT HERE] What do I put after the questionmark in order to have the browser (or a C++ http component) ca...

Java: Mock testing probably with Mockito

I think I'm not using verify correctly. Here is the test: @Mock GameMaster mockGM; Player pWithMock; @Before public void setUpPlayer() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { pWithMock = new Player(mockGM); } @Test public void mockDump() { pWithMock.testDump(); ...

Moq - verifying a method was called

Using Moq, I have a very odd issue where the setup on a mock only seems to work if the method I am setting up is public. I don't know if this is a Moq bug or if I just have this wrong (newbie to Moq). Here is the test case: public class TestClass { public string Say() { return Hello(); } inte...

Using JMockit to mock autowired interface implementations

We are writing JUnit tests for a class that uses Spring autowiring to inject a dependency which is some instance of an interface. Since the class under test never explicitly instantiates the dependency or has it passed in a constructor, it appears that JMockit doesn't feel obligated to instantiate it either. Up until now we have been us...

How do I mock/fake the session object in ASP.Net Web forms?

Is there a way to mock/fake the session object in ASP.Net Web forms when creating unit tests? I am currenclty storing user details in a session variable which is accessed by my business logic. When testing my business logic, in isolation, the session is not available. This seems to indicate a bad design (though im not sure). Should the...

c# Rhino mocks - Is this an appropriate use of mocks?

Forgive me for my ignorance. I'm trying to learn Rhino. public Foo(Stream stream) { if (stream == null) throw new ArgumentNullException("woot"); if (!stream.CanRead) throw new NotSupportedException("sporkish"); if (!stream.CanSeek) throw new NotSupportedException("monkey"); } I would like to test this function with a NUni...

Mock testing with Repository pattern example.

I'm refactoring existing class to support Products import from CSV file.   Requirements: 1) during import products are identified by special product number. 2) product has category attribute. If such category exist - use its id, if not - create it first. 3) if product with some number already exists in DB - do an update of other fie...