unit-testing

Unit Testing with functions that return random results

I don't think that this is specific to a language or framework, but I am using xUnit.net and C#. I have a function that returns a random date in a certain range. I pass in a date, and the returning date is always in range of 1 to 40 years before the given date. Now I just wonder if there is a good way to unit test this. The best approa...

Unit testing destructors?

Is there any good way to unit test destructors? Like say I have a class like this (contrived) example: class X { private: int *x; public: X() { x = new int; } ~X() { delete x; } int *getX() {return x;} const int *getX() const {return x;} }; Is there any good way to unit test th...

How did you adapt your unit tests to deal with changing requirements?

I have a project where I've been using TDD and unit tests as "software vises". In essence I translate the requirements into tests that verify that the code conforms to the requirements. I rarely have to go back and edit the unit tests, which rather is the point: only the "real" code should be modified. At the moment, there are 900 unit t...

What is the right balance between unit vs. functional testing in a typical web application?

Unit tests are cheaper to write and maintain, but they don't cover all scenarios. What is the right balance between them? ...

Unit testing fake repository, how can I test the GetById method without first adding an entity

My understanding is that you have to write unit tests that isolate functionality. So given a repository class that has this method: Entity GetById(Guid id) and a fake implementation (using a Dictionary for storage), how would you write a test without first adding an entity? Is it ok to use a known set of guids for testing? Then in the...

Why would I write a fake class and unit test it?

I understand the need to test a class that has logic (for instance, one that can calculate discounts), where you can test the actual class. But I just started writing unit tests for a project that will act as a repository (get objects from a database). I find myself writing a 'fake' repository that implements an ISomethingRepository in...

How do I perform a Unit Test using threads?

Executive Summary: When assertion errors are thrown in the threads, the unit test doesn't die. This makes sense, since one thread shouldn't be allowed to crash another thread. The question is how do I either 1) make the whole test fail when the first of the helper threads crashes or 2) loop through and determine the state of each thread ...

Testing Authenticity Token in Rails

Hi, I'm trying to test a method I have in my application, but I don't know how to unit test a method that is being protected from forgery, take a look at this: def index @alumnos = Alumno.paginate :per_page => 20, :page => params[:page], :order => :nombre respond_to do |format| format.html # index.html.erb ...

Mocking for Dummies?

I'm new to mocking, I have a new .net web project that is in UI->BLL->DAL->DB structure, I use NUnit to do some testing currently. I intent to use it to test middle tier so I don't have to actually write to DB. Now, I've never done any mocking, don't quite know where to start, so I am looking for a mocking framework that has some end to...

How to design code for testability

I have been looking at using TDD and implementing proper testing (only just started to learn how much better it makes your life) in any of my projects that I create in the future. So for the last couple of days I have been floating around on SO trying to learn about how to design your application for testability, but I still seem to be ...

How to Unit Test a missing .NET ConfigurationSection

If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName) as XALServiceConfiguration; if (config == null) throw new ConfigurationErrorsException("Configuration element 'xalService' was not foun...

TDD and ADO.NET Entity Framework

I've been playing with ADO.NET Entity Framework lately, and I find that it suits my needs for a project I'm developing. I also find cool its non-invasive nature. After generating a data model from an existing database you are faced with the task of integrating the generated model and your business logic. More specifically, I'm used to i...

Best strategy to get coding prepared for unit testing

I have a solution that is missing a lot of code coverage. I need to refactor this code to decouple to begin to create unit tests. What is the best strategy? I am first thinking that I should push to decouple business logic from data access from businessobjects to first get some organization and then drill down from there. Since many ...

How to read ".NET Data Provider for SqlServer" performance counters from MS Test?

How do I read from the performance counter "NumberOfActiveConnections" of the category ".NET Data Provider for SqlServer" from a unit test in MS Test? I'm trying the following but it seems like I get the instance name wrong. The MSDN documentation claims this is a correct way of getting the instance name of a WinForms app but this won't...

Compare equality between two objects in NUnit

I'm trying to assert that one object is "equal" to another object. The objects are just instances of a class with a bunch of public properties. Is there an easy way to have NUnit assert equality based on the properties? This is my current solution but I think there may be something better: Assert.AreEqual(LeftObject.Property1, RightO...

Automatically setting up unit test projects in VS2008, need help

I'm beginning to toy with the new ASP.NET MVC framework, and reading around in some tutorials I saw that in addition to creating the MVC project, theres an option to add a Unit Test project, using the Test framework (which I have) thats basically already set up for MVC testing. Its supposed to pop up when you create a new MVC project, an...

Mocking attributes - C#

I use custom Attributes in a project and I would like to integrate them in my unit-tests. Now I use Rhino Mocks to create my mocks but I don't see a way to add my attributes (and there parameters) to them. Did I miss something, or is it not possible? Other mocking framework? Or do I have to create dummy implementations with my attribut...

When not / to write tests before implementation?

Title says it all (mostly). I'm not sure how "tests first" works and I'd like to hear arguments about when and why one would take this approach. I hear that it's often recommended to write tests and mock things before writing a single line of implementation. However, I can't help but think that it doesn't fit every situation. For insta...

How do I test database-related code with NUnit?

I want to write unit tests with NUnit that hit the database. I'd like to have the database in a consistent state for each test. I thought transactions would allow me to "undo" each test so I searched around and found several articles from 2004-05 on the topic: http://weblogs.asp.net/rosherove/archive/2004/07/12/180189.aspx http://weblo...

RhinoMocks Event Subscription

Being new to RhinoMocks and Unit Testing, I have come accross an issue that I cannot seem to find a resolution to (no matter how much documentation I read). The issue is this: I have created an Interface that exposes 5 Events (to be used for a view in ASP.NET and the MVP Supervisory Controller pattern..... I know, I should be using M...