mocking

How do I mock the Python method OptionParser.error(), which does a sys.exit()?

I'm trying to unit test some code that looks like this: def main(): parser = optparse.OptionParser(description='This tool is cool', prog='cool-tool') parser.add_option('--foo', action='store', help='The foo option is self-explanatory') options, arguments = parser.parse_args() if not options.foo: parser.error('--f...

Unit testing and mocking email sender in Python with Google AppEngine

I'm a newbie to python and the app engine. I have this code that sends an email based on request params after some auth logic. in my Unit tests (i'm using GAEUnit), how do I confirm an email with specific contents were sent? - i.e. how do I mock the emailer with a fake emailer to verify send was called? class EmailHandler(webapp.Reques...

Should I create a interface and mock this class.

I have this class called Table: class Table { public string Name { get { return this.wrapper.Eval(//some command); //wrapper is pass in by the ctor and is a COM object. } } } which is used in this class: class Map { public Map MapTable(Table table) { return new Map...

How to set up NUnit Mock Object for IGrouping

public myReturnObj MethodA(System.Linq.IGrouping<string, MyObject> group){ ... foreach (MyObject o in group) { //business process } ... return myReturnObj; } I want to set up NUnit Mock object for passing as a paramter and then check the result of MethodA in my unittest. How do I mock this IGrouping? ...

How do I write a Mock Object?

My first programming job introduced me to unit testing and the concept of mock objects, but something always felt wrong about it. Let's say we were writing a bank app, and needed to mock a BankAccount object: // boilerplate code public interface IBankAccount { void Deposit(int amount); void Withdrawal(int amoun...

NHibernate testing, mocking ISession

Hi, I am using NHibernate and Rhinomocks and having trouble testing what I want. I would like to test the following repository method without hitting the database (where _session is injected into the repository as ISession): public class Repository : IRepository { (... code snipped for brevity ...) public T FindBy<T>(Expressio...

How to unit-test a file writing method with Visual Studio's built-in automated tests?

I use Visual Studio 2008 Professional automated tests. I have a function that writes to a file. I want to unit test the file writing function. I have read somewhere that I would have to mock a file somehow. I don't know how to do it. Can you help? How to unit-test a method that downloads a page from the Internet? ...

TDD: How would you work on this class, test-first style?

Hi there, I am writing a small app to teach myself ASP.NET MVC, and one of its features is the ability to search for books at Amazon (or other sites) and add them to a "bookshelf". So I created an interface called IBookSearch (with a method DoSearch), and an implementation AmazonSearch that looks like this public class AmazonSearch : ...

Mock set indexer in Moq

I can do: var req = new Mock<HttpRequestBase>(); var vals = new Dictionary<string, string>(); req.Expect(r => r[It.IsAny<string>()]) .Returns(s => vals[s]); But what about something like this ??? req.ExpectSet(r => r[It.IsAny<string>()], It.IsAny<string>()) .Callback((s, v) => vals[s] = v); ...

What are the differences between mocks and stubs on Rhino Mocks?

I haven't play enough with this and usually use mocks, but I wonder what are the differences between this two and when to use one or the other on Rhino Mocks. Update: I also found the answer to my question in Ayende's words: The difference between stubs and mocks You can get the actual definition of the these terms in this article: Mo...

Is there any C# Dynamic Mock framework available for Silverlight?

I want to use dynamic mocks when testing a Silverlight application. I have tried Moq and Rhino but these frameworks assemblies cannot be added to a silverlight project as they are incompatible with the silverlight runtime. Is there an existing silverlight mock framework (or patch for moq) that will allow me to use mock objects in a sil...

Rhino Mocks, void and properties

Just starting out with Rhino Mocks and im having a very simple problem, how do I mock a class with a void which sets a property? class SomeClass : ISomeClass { private bool _someArg; public bool SomeProp { get; set; } public SomeClass(bool someArg) { _someArg = someArg; } public void SomeMethod() ...

How can I use Record/Playback syntax with StructureMap AutoMocker?

Is it possible to use the syntax using(_mocks.Record()) { //... } using(_mocks.Playback()) { //... } with StructureMap RhinoAutoMocker? In Jeremy Millers original post AutoMocker in StructureMap 2.5 this seems possible since RhinoAutoMocker inherits MockRepository, but in version 2.5.2 of StructureMap this seems to be implemen...

How to properly mock and unit test

I'm basically trying to teach myself how to code and I want to follow good practices. There are obvious benefits to unit testing. There is also much zealotry when it comes to unit-testing and I prefer a much more pragmatic approach to coding and life in general. As context, I'm currently writing my first "real" application which is the u...

Rhino Mocks - Stub .Expect vs .AssertWasCalled

Disclosure: I am a Rhino Mocks n00b! OK, I know there has been a lot of confusion over the new AAA syntax in Rhino Mocks, but I have to be honest, from what I have seen so far, I like. It reads better, and saves on some keystrokes. However, I am having problems getting my head around this, and seek some advice to make sure I am not mis...

TDD: Stub, Mock, or None of the Above

Hello, I'm trying to learn TDD by applying it to a simple project of mine. Some details (and an earlier question) are here: http://stackoverflow.com/questions/473679/tdd-help-with-writing-testable-class The specifics are I have a PurchaseOrderCollection class that has a private List of PurchaseOrders (passed in at constructor), and t...

Database data needed in integration tests; created by API calls or using imported data?

This question is more or less programming language agnostic. However as I'm mostly into Java these days that's where I'll draw my examples from. I'm also thinking about the OOP case, so if you want to test a method you need an instance of that methods class. A core rule for unit tests is that they should be autonomous, and that can be a...

DAL code generator that can handle mocking and testing, .NET

Hi, I have been testing the nusoft kinetic framework and have been pretty satisfied with it until now. I was going to make some tests with MbUnit and Rhino Mock when I realized that the hole framework was built with static methods and protected classes, making this untestable. My question is, does any one know about a framework like the...

Are there any mocking libraries that support the .NET Compact Framwork.

I was just wondering if anyone has found a mocking library that can be used with the .NET compact framwork. I have tried Moq but it doesn't seem to work, I did a quick Google search but couldn't find anything useful. Thanks. ...

Should I be using a moq just to test if a method is called?

I have some test code that looks like this: [Test] public void RunTableInfoCommandShouldCallTableINfoWithName() { string expectedcommand = "TableInfo(TestTable,1)"; Table.RunTableInfoCommand(mockmapinfo.Object, "TestTable", TableInfoEnum.TAB_INFO_NAME); //This is just an en...