mocking

What is the difference between Moq-ing a class or interface?

I've been using moq to mock objects in my unit tests and I've seen on the site about moq that it is able to mock both classes and interfaces. I had a discussion with one of my work mates the other day and they stated that there is never a reason to mock classes and I should only mock interfaces. I didn't really have an answer to that.....

How do I mock an IMAP server in Python, despite extreme laziness?

I'm curious to know if there is an easy way to mock an IMAP server (a la the imaplib module) in Python, without doing a lot of work. Is there a pre-existing solution? Ideally I could connect to the existing IMAP server, do a dump, and have the mock server run off the real mailbox/email structure. Some background into the laziness: I ha...

Is there a way in JMockit to call the original method from a mocked method?

In my mock class, I'm mocking method foo(). For some test cases, I want the mock implementation of foo() to return a special value. For other test cases, I want to use the real implementation of foo(). I have a boolean defined in my mock class so that I can determine in the mock method whether I want to return the special value, or us...

Moq tests using ExpectSet() with It.Is<T>() aren't behaving as... expected.

I've isolated the behaviour into the following test case. I'd be grateful to anyone who can tell me how to expect/verify a property set for a List<T> property - it appears there's something going on inside It.Is<T>(predicate) that isn't making a whole lot of sense to me right now. Sample code will run as a console app from VS2008 - you'l...

Mocking Database call Asp.net MVC application

How can I mock the database calls to make my application logic been tested without database? ...

How do you mock your repositories?

I've use Moq to mock my repositories. However, someone recently said that they prefer to create hard-coded test implementations of their repository interfaces. What are the pros and cons of each approach? Edit: clarified meaning of repository with link to Fowler. ...

Unit testing Event Handlers in SharePoint?

Is it even possible? So far, I found that we can buy TypeMock to mock the SharePoint objects and then use any free Mocking framework (Moq?) to do the rest of the job. What do you think? It seams that without TypeMock, it's impossible to do unit test within SharePoint. To properly test our events, we need to give the event a SPItemEve...

How can I unit test my ASP.NET MVC controller that uses FormsAuthentication?

I'm working with a ASP.NET MVC solution in a test driven manner and I want to login a user to my application using forms authentication. The code I would like to end up with in the controller looks something like this: FormsAuthentication.SetAuthCookie(userName, false); My question is how do I write a test to justify this code? Is th...

Using mock objects outside of testing, bad practice?

I'm working on a project where there is a lot of external service messaging. A good way to describe it in only a slightly "hyperbolas" way would be an application where the system has to send messages to the Flicker API, the Facebook API, and the Netflix API. To support disconnected scenarios, logging concerns, developer usability, conf...

RSpec mocking an :each block

I want to use RSpec mocks to provide canned input to a block. Ruby: class Parser attr_accessor :extracted def parse(fname) File.open(fname).each do |line| extracted = line if line =~ /^RCS file: (.*),v$/ end end end RSpec: describe Parser before do @parser = Parser.new @lines = mock("lines") @lines...

Separate Class vs Method

Quick design question. ClassA has a method called DoSomething(args) In DoSomething(), before it can actually do something, it needs to do some preparatory work with args. I believe this should be encapsulated within ClassA, (as opposed to doing the prep work outside and passing it in) as nothing else needs to know that this prep work ...

Rhino Mocks: AAA test syntax without static MockRepository methods?

I have been using (and liking) the new Rhino Mocks AAA syntax. However, one thing that puzzles me is that I have to create my stubs and mocks like this: var v1 = MockRepository.GenerateStub<MyClass>(); instead of with an instantiated MockRepository: var mr = new MockRepository(); var v1 = mr.GenerateStub<MyClass>(); This syntax wo...

Mocking GetEnumerator() method of an IEnumerable<T> types

The following test case fails in rhino mocks: [TestFixture] public class EnumeratorTest { [Test] public void Should_be_able_to_use_enumerator_more_than_once() { var numbers = MockRepository.GenerateStub<INumbers>(); numbers.Stub(x => x.GetEnumerator()).Return(new List<int> ...

Mocking HTTP Server

Hello, I would like to write tests for my client code, which accesses HTTP Server. I am looking for simple HTTP Server, which would simulate responses from real server. Such HTTP Server (mock server :-)) should verify all requests really came from my client code verify that requests had all required parameters send response, ideally...

Strategies for Using Mock Objects when Unit Testing DAOs

I am curious what strategies folks have found for unit testing a data access class that does not involve loading (and presumably unloading) a real database for each test method? Are you using mock objects to represent the database connection? If so, are you required to pass the mock object into every method-under-test, and thus forcing t...

Easy way to mock a WCF service?

I've got an app which is using a WCF service. Now I'd like to add unit tests to the app. For some cases I need to mock the WCF service, since getting the desired behaviour from the service sometimes is tough (e.g. service throws special exceptions). I could add yet another interface to the wcf client, but this seems a bit silly, since ...

Using mocking to test derived classes in Python.

I have code that looks like this: import xmlrpclib class Base(object): def __init__(self, server): self.conn = xmlrpclib.ServerProxy(server) def foo(self): return self.conn.do_something() class Derived(Base): def foo(self): if Base.foo(): return self.conn.do_something_else() How shoul...

How to mock ObjectContext or ObjectQuery<T> in Entity Framework?

How to mock ObjectContext or ObjectQuery in Entity Framework? ...

Stubbing functions in simulations

I'm working on an embedded C project that depends on some external HW. I wish to stub out the code accessing these parts, so I can simulate the system without using any HW. Until now I have used some macros but this forces me to change a little on my production code, which I would like to avoid. Example: stub.h #ifdef _STUB_HW #define ...

How to avoid duplicate code when using mocks in unittests

I am using dependency injection to supply mocks for code outside of my class under test. I find myself writing alot of the same code over and over as I need to mock out AuthProvider, ConfigurationManager, etc. which are used in the method I want to test. The method contains branches (if-then-else) and therefore I have multiple tests in p...