mocking

Mocking ResourceManager in .NET

We have a small wrapper class that uses ResourceManager to load string resources from assemblies. We also have some unit tests that check it loads the correct details from the correct assemblies for different cultures, assemblies etc. Our tests are therefore currently dependant upon resources we have constructed for the test. Is the...

How do I write useful unit tests for a mostly service-oriented app?

I've used unit tests successfully for a while, but I'm beginning to think they're only useful for classes/methods that actually perform a fair amount of logic - parsers, doing math, complex business logic - all good candidates for testing, no question. I'm really struggling to figure out how to use testing for another class of objects: t...

Is verify called automatically on Groovy's MockFor

According to the javadocs, Groovy's MockFor object always ends with a verify. Its StubFor docs say calling verify is up to the user. I read that as saying that verify will automatically be called on the MockFor object. However, in looking at the groovy samples that use MockFor on a Java object (http://svn.codehaus.org/groovy/trunk/groovy...

Code Contract, Interfaces And Mocking

Hi all! I have a code contract on some interface IImageRepository: [ContractClassFor(typeof(IImageRepository))] sealed class IImageRepositoryContract : IImageRepository { IImage IImageRepository.GetById(int imageId) { Contract.Requires(imageId > 0); return default(IImage); } } I want to test code contract...

Problem mocking events attached to abstract class through constructor of other object

i have an abstract class and im trying to mock out events being attached to it using Rhino Mocks. Here a bit of the abstract class public abstract class Download { public virtual event EventHandler<DownloadProgressEventArgs> DownloadProgress_Changed; protected virtual void OnDownloadProgressChanged(DownloadProgressEventargs e) ...

What's the best way to fill POCOs with dummy data?

I have a bunch of POCOs that all relate to each other in a big tree. For example, this is the top-level element: public class Incident : Entity<Incident> { public virtual string Name { get; set; } public virtual DateTime Date { get; set; } public virtual IEnumerable<Site> Sites { get; set; } public Incident() { ...

Basic NMock database examples for CRUD application

I'm looking for some basic examples of using NMock2 to mock database calls for a CRUD application. Thanks, Chris ...

Rspec Mocks: mock / yield the block from a method call

I've got this code: Net::SSH.start(@server, @username, :password => @password) do |ssh| output = ssh.exec!(@command) @logger.info 'SSH output: ' @logger.info output end I can mock the SSH.Start using RSpec's mock framework like this, to tell me that I've started the SSH session: Net::SSH.should_receive(:start)....

rspec mocks: verify expectations in it "should" methods?

I'm trying to use rspec's mocking to setup expectations that I can verify in the it "should" methods... but I don't know how to do this... when i call the .should_receive methods on the mock, it verifies the expected call as soon as the before :all method exits. here's a small example: describe Foo, "when doing something" do before :a...

How to test the function behavior in unit test?

Hi Everyone, If a function just calls another function or performs actions. How do I test it? Currently, I enforce all the functions should return a value so that I could assert the function return values. However, I think this approach mass up the API because in the production code. I don't need those functions to return value. Any go...

Mocking inter-method dependencies

I've recently started using mock objects in my tests, but I'm still very inexperienced with them and unsure of how to use them in some cases. At the moment I'm struggling with how to mock inter-method dependencies (calling method A has an effect on the results of method B), and whether it should even be mocked (in the sense of using a mo...

Going From State Verification to Behavioral Verification using MOQ

I am trying to embrace TDD and started learning about mocking. I need some advice on what i should test and how to make my classes more behavioral and not simple data containers (with a bunch of getters/setters). Consider this class. public class Post { List<Comment> Comments {get; private set;} public void AddComment(string me...

unit testing/ mocking static event handlers, what are the available options?

Im fully aware of the "problem" with static event handlers from a GC perspective so i'm not looking for advice on "DONT use static events" or anything like that, in my scenario this isnt a concern. I have a static class which has an event declared public static event EventHandler<MyEventArgs> FilePickedUpFromStorage; i have a client ...

how to mock a parametrized constructor?

I've the following class. It has the code to connect to SAP in its constructor. There is an abstract method(the subclasses define the implementation) which I want to mock. public abstract class BapiExecutor { ... public BapiExecutor(final SapConnectionInfo connectionInfo) throws java.lang.Exception { if (!vali...

Why is it so bad to mock classes?

I recently discussed with a colleague about mocking. He said that mocking classes is very bad and should not be done, only in few cases. He says that only interfaces should be mocked, otherwise it's an architecture fault. I wonder why this statement (I fully trust him) is so correct? I don't know it and would like to be convinced. Did...

how to write tests without so many mocks?

I am a heavy advocate of proper Test Driven Design or Behavior Driven Design and I love writing tests. However, I keep coding myself into a corner where I need to use 3-5 mocks in a particular test case for a single class. No matter which way I start, top down or bottom up I end up with a design that requires at least three collaborators...

NUnit Mocking not working for Singleton Method

Bear with me, I'm new to NUnit. I come from the land of Rails, so some of this is new to me. I have a line of code that looks like this: var code = WebSiteConfiguration.Instance.getCodeByCodeNameAndType("CATALOG_Brands_MinQty", item.Catalog); I'm trying to mock it, like this (assume code is already initialized): var _websiteConfigu...

Are mock frameworks and high test coverage important?

Mock frameworks, e.g. EasyMock, make it easier to plugin dummy dependencies. Having said that, using them for ensuring how different methods on particular components are called (and in what order) seems bad to me. It exposes the behaviour to test class, which makes it harder to maintain production code. And I really don't see the benefit...

Faking an RS232 Serial Port

I'm developing a project that has a number of hardware sensors connecting to the deployment machine through RS232 serial ports. But ... I'm developing on a machine without an physical RS232 serial ports, but I would like to make fake serial ports that I can connect to and output data from with the aim of faking input from hardware senso...

Unit testing a class with no return value?

Hello, I didn't find much in tutorials on this specific question.. So I have a class called 'Job' which has public ctors and a single public Run() function. Everything in the class is private and encapsulated in the class. (You may remember an older post here on this http://stackoverflow.com/questions/1590953/testing-only-the-public-me...