mocking

Unit test HttpContext.Current.Cache or other server-side methods in C#?

When creating a unit test for a class that uses the HttpContext.Current.Cache class, I get an error when using NUnit. The functionality is basic - check if an item is in the cache, and if not, create it and put it in: if (HttpContext.Current.Cache["Some_Key"] == null) { myObject = new Object(); HttpContext.Current.Cache.Insert(...

How do you test methods that take enums.

I already have a test setup up for this function but I would like to know if it's the best way to go about it. I have a method like this: public static string RunTableInfoCommand(IMapinfoWrapper wrapper, TableInfoEnum infoToReturn) { //pass the int value of infoToReturn to underlying COM objec...

Should I be testing a methods implementation using mocks.

I'm having a bit of trouble doing some unit-testing using moq. If I have a function like this: public string GetName(IMapinfoWrapper wrapper) { return wrapper.Evaluate("My com command"); ///"My comm command" is the same all the time. } Then I have a test that checks the return value of the GetName function: [Test] public void ...

Django: How to create a model dynamically just for testing

I have a Django app that requires a settings attribute in the form of: RELATED_MODELS = ('appname1.modelname1.attribute1', 'appname1.modelname2.attribute2', 'appname2.modelname3.attribute3', ...) Then hooks their post_save signal to update some other fixed model depending on the attributeN defined....

How do I mock a method with generic objects in JMockit?

This question is self explanatory if you know how to use JMockit: How do I mock a method that has generics on it? I want to mock this method: public T save(T entity) but it always throws an exception like this: mockit.RealMethodNotFoundForMockException: Corresponding real methods not found for the following mocks: Object save(Object) ...

Use Mockito to verify that nothing is called after a method

I'm using Mockito to write a unit test in Java, and I'd like to verify that a certain method is the last one called on an object. I'm doing something like this in the code under test: row.setSomething(value); row.setSomethingElse(anotherValue); row.editABunchMoreStuff(); row.saveToDatabase(); In my mock, I don't care about the order ...

Should I write unit test for everything?

Hi, I am wondering should I write unit test for everything. There are some classes is very difficult to write unit test. For example, I am writing some program for handling audio. The class for capturing audio from microphone, and class for play audio to speaker, how can I write unit test for those classes? I can't get output and input ...

Is there a way to decide when a RhinoMocks mock starts recording?

As I understand it, a mock object created with RhinoMocks enter the recording state directly when it is created, and then you call Replay() to enter the replay state. I would like to manually decide when the mock object starts recording, or be able to pause the recording. Would that be possible in RhinoMocks? Thanks /Erik ...

Mocking Static methods using Rhino.Mocks

Is it possible to mock a static method using Rhino.Mocks ? if Rhino does not support this.. Is there a pattern or something which would let me accomplish the same ? ...

Mocking a HttpContext Response.Output with Moq

I've been using the MvcMockHelpers class found at Hanselman's blog for passing in a mocked HttpContext. We extended it somewhat to add some authentication data we needed and for the most part this has been great. The issue we are having is that the context we are giving to the controller has a null value in the HttpContext.Response.Outp...

How to mock a method (custom behavior) with Rhino Mocks in VB.NET

How can I mock one method with RhinoMocks in VB.Net? The reference I found is in C#: Expect.Call(delegate{list.Add(0);}).IgnoreArguments() .Do((Action<int>)delegate(int item) { if (item < 0) throw new ArgumentOutOfRangeException(); }); SharpDevelop converts this to: Expect.Call(Function() Do list.Add(0) ...

Mock Struts FormFile

I have an action class that expects a FormFile from a DynaActionForm. Typically in JMock, I'll just add request params to the struts mock, which will in turn populate the DynaActionForm. Obviously, the set request param is for strings only. So I'm unsure how to get or set the mock to use a multi-part request such that I can add the Fo...

TDD with Entity, mocking the generated Entity classes?

My apologies, I read this post: http://stackoverflow.com/questions/316897/tdd-and-ado-net-entity-framework But I don't think it covers what I'm looking for and other similar questions seem unanswered. So, forgive me if this has already been answered. I have an application that I'm writing. I've created some Entity classes. I want to m...

What is the best way to unit test a EJB3 component without having to deploy the component.

I would like to have a JUnit (or other) system where the enterprise beans can be tested in a mock environment so that all resources/beans and so on are injected locally. The enterprise beans should not have to accomplish this. All kinds of injection should be supported. I would like to have a maven plugin for this so that the tests can...

Easily mockable HTTP client framework for C#

Hi! In a upcoming project I'm going to write an application in C# which partly has to communicate with a HTTP server. I'm very fond of writing my code TDD-style, and I would just love it if I could mock all of the HTTP requests in my tests. Does any one here know about an easly mockable HTTP client framework? Ps. I usually use Moq for...

How can mocking external services improve unit tests?

I'm connecting to a simple, if idiosyncractic, external service. I believe that my unit tests should not depend on the availability or implementation of that external service, so I intend to mock it out. I need the mock to accept and return realistic messages and responses - otherwise my tests won't represent real states of affairs. Fo...

How do I use Moq to mock an extension method?

I am writing a test that depends on the results of an extension method but I don't want a future failure of that extension method to ever break this test. Mocking that result seemed the obvious choice but Moq doesn't seem to offer a way to override a static method (a requirement for an extension method). There is a similar idea with Moq....

Testing helper methods in Rails

I'm testing my rails helper methods like this: require File.dirname(__FILE__) + '/../../test_helper' require 'action_view/test_case' require 'action_view/helpers' class ListingsHelperTest < ActionView::TestCase def setup @controller = ListingsController.new @request = ActionController::TestRequest.new @response = Action...

Easiest way to learn mocking in C#?

Please advise on the easiest learning path, as well as simple mocking library to start with. ...

Mocking a URL in Java

We have a URL object in one of our Java classes that we want to mock, but it's a final class so we cannot. We do not want to go a level above, and mock the InputStream because that will still leave us with untested code (we have draconian test coverage standards). I've tried jMockIt's reflective powers but we work on Macs and there are ...