mocking

Creating TCP network errors for unit testing

I'd like to create various network errors during testing. I'm using the Berkely sockets API directly in C++ on Linux. I'm running a mock server in another thread from within Boost.Test which listens on localhost. For instance, I'd like to create a timeout during connect. So far I've tried not calling accept in my mock server and sett...

asp.net mvc How to test controllers correctly

Hi, I'm having difficulty testing controllers. Original my controller for testing looked something like this: SomethingController CreateSomethingController() { var somethingData = FakeSomethingData.CreateFakeData(); var fakeRepository = FakeRepository.Create(); var controller = new SomethingController(fakeRepository); ...

Integrate Pex with MoQ

Can anyone point me to a resource that shows an example of how Pex can be used in conjunction with MoQ? Thanks ...

Calling original method with Moq

I have a ProductRepository with 2 methods, GetAllProducts and GetProductByType, and I want to test the logic at GetProductByType. Internally, GetProductByType makes a call to GetAllProducts and then filters the correct ones. public virtual IEnumerable<Product> GetAllProducts() { //returns all products in memory, db etc } public vir...

Testing event handling with mocks using nunit

I need to create some unit tests that confirm that instance of a class responds appropriately to an event raised by another object. Passing in a mock of the 'watched' object is easy enough, but as far as I can tell, mocks in nunit don't provide a means of having the mock raise an event. I'm considering using reflection to directly invok...

PHPUnit Stubbing Class methods declared as "final"

Hi, I'm writing a unit test for a class method that calls another class's method using a mock, only the method that needs to be called is declared as final, so PHPUnit is unable to mock it. Is there a different approach I can take? example: class to be mocked class Class_To_Mock { final public function needsToBeCalled($options) ...

cheap way to mock an interface with no runtime overhead

Suppose I have an interface with lots of methods that I want to mock for a test, and suppose that I don't need it to do anything, I just need the object under test to have an instance of it. For example, I want to run some performance testing/benchmarking over a certain bit of code and don't want the methods on this interface to contribu...

Unit Testing results that are retrieved by a processed Thread

I have a unit test in which I mock (with moq) an object and let it verify if it executed a method properly. This method is being executed in a Thread that I create in my SUT (System under Test). When I want to do VerifyAll() on the Mock it could happen that the Thread is still running and that it isn't finished yet executing the method -...

Returning different results or throwing exceptions from successive calls to Moq Mock

I get a Moq object to return different values on successive calls to a method. This is done by this extension method: public static void ReturnsInOrder<T, TResult>(this ISetup<T, TResult> setup, params TResult[] results) where T : class { setup.Returns(new Queue<TResult>(results).Dequeue); } Now I want one of the calls to throw a...

Can someone help me Mock this ASP.NET MVC Controller with Moq, please?

Hi folks, I'm trying to mock an ASP.NET MVC2 Controller using Moq but I get an error because i'm trying to mock an non-overridable property. How should I be doing this, please? NOTE: the controller I'm trying to mock up is the (abstract) ASP.NET MVC2 Controller ... not a custom controller. Why? I'm trying to test some custom controller...

EasyMock - how to reset mock but maintain expectations?

Is it possible to re-define specific expectations on the same instance of mock object? Say I have this test which verifies OK: List<String> foo = createMock(List.class); expect(foo.get(1)).andReturn("Wibble").once(); expect(foo.size()).andReturn(1).once(); replay(foo); System.out.println(foo.get(1)); System.out.println(foo.size()); ver...

DynamicObject: is this a good solution for mocking?

I've been learning about DynamicObject in .NET 4.0 and was wondering if this type would be well suited to creating mock objects. Mocking seems like a great way to use DynamicObject, but am I missing something? Are there any mocking frameworks that use DynamicObject (as opposed to dynamic proxies or interception) for mocking? Are there...

Mock Objects vs Test Database

What's the advantage of using mock objects in comparison to a static test database that has known data and using transactions to make sure nothing changes when testing against the database. ...

How to write Internal mockable method

We are using Moq as our mocking framework, the problem is that type that needs to be mock-able is done using an interface, the problem with that is anything in that interface will be public and therefore considered part our public API. is there a way to have to have a member that is mockable and not public? ...

BDD and functional tests

I am starting to buy into BDD. Basically, as I understand it, you write scenario that describes well acceptance criteria for certain story. You start by simple tests, from the outside-in, using mocks in place of classes you do not implement as yet. As you progress, you should replace mocks with real classes. From Introduction to BDD: ...

How to setup a simple Unit Test with Moq?

I'm new to Moq and not quite sure why this won't run. Repository Interface using System.Collections.Generic; public interface IRepository { IEnumerable<string> list(); } Service Interface using System.Collections.Generic; public interface IService { IEnumerable<string> AllItems(); } Service Class using System.Collection...

Unit tests and database

This question about unit tests sparked another thing that's been bothering me. I've gone back and forth on three ways to do unit tests when hitting a database. Create mock objects and plug them in. This has the advantage of not needing a database, but it's time consuming and I'm not sure how much return on investment I'm getting. I've ...

Mockito preferrable over easymock?

Hi there, recently I made the switch to Mockito framework and am very happy with it (see also blog-post). The switch from easymock to Mockito was very straightforward and I managed to make the tests down compatible (i.e. test cases behave the same). Do you see real reasons or shootout criteria to prefer easymock over Mockito? So far of...

Unit testing object construction/initialization.

I have a class Foo that uses another class Bar to do some stuff. I'm trying to do test driven development, and therefore am writing unit tests for Foo to make sure it calls the appropriate methods on Bar, and for this purpose I'm using dependency injection and mocking Bar (using Rhino Mocks). E.g. (in C#): class Foo { private IBar b...

Boost.Test and Forking

I'm using Boost.Test for Unit testing and am currently running various mock servers in separate threads which get launched from within each test. In order to more accurately test my code the mock server's should really be in separate processes. I was thinking about doing something along these lines: MY_TEST() if (fork() == 0) { r...