Ayende @ Rahien

Refunds available at head office

How many tests?

Here is an interesting question, how many tests do you need for the following method?

///<summary> 
///Get the latest published webcast 
///</summary>
public Webcast GetLatest();

Here is how the Webcast entity looks like:

image

Any thoughts?

Comments

Charles
06/10/2008 03:38 PM by
Charles

None, good programmers write perfect code first time.

JacobM
06/10/2008 03:49 PM by
JacobM

Enough to ensure that you are correctly getting the Webcast that has the most recent PublishDate, not simply the one that happens to have been most recently added to wherever the Webcasts are stored. Maybe make sure it works when there are no Webcasts stored, when there is one Webcast, and when there are multiple Webcasts.

Here's a question -- what if the PublishDate is in the future? Should it be returned? If not, that's another test.

What happens if two Webcasts have the same PublishDate? Which one is "the latest"? Whatever the answer, there's a scenario for another test.

Chris
06/10/2008 04:20 PM by
Chris

@JacobM - Yet another case is what should GetLatest() do if (!PublishDate.HasValue)?

Cory Foy
06/10/2008 04:28 PM by
Cory Foy

It's a trick question. It takes as many tests as is necessary to drive out the design.

I'd probably start with something like:

public void GetLatestWebcastCallsGetLatestOfDAO();

since we'll have some sort of data access. I'd also likely have tests in place for equality and hashCode of the Webcast object itself (though it could be abstracted away).

Then, for the DAO, I'd likely have tests in place to make sure that we were calling the data layer correctly, probably by using an in-memory DB. Then we'd need integration tests to make sure everything was working correctly.

The whole nullable DateTime seems interesting - but it also seems like it would be abstracted by the data access layer. If we store the webcasts in a file system, then we have at least the create or last modified date. If they are in the database, then we could use the max primary key, or filter out those records which don't have a publish date.

So, filtering out the test cases for Webcast itself, and the data access layer, then we'd need one - an interaction-based tests which verifies that GetLatestWebcast calls the appropriate DAL method.

Zack Owens
06/10/2008 04:29 PM by
Zack Owens

It depends on what the GetLatest() method's dependencies are. You probably don't have to test the Webcast class unless there is actually code that you have written.. if it's just a bunch of properties, there is no need to test whether the .NET framework does its job.

James Curran
06/10/2008 05:29 PM by
James Curran

It depends on the purpose of the class this method comes from. Is it the ultimate source for information on Webcasts, or is it just your local access point for it.

In other word, does this class hold a handle to a database connection or a RSS feed; or is it the database?

If it is the ultimate source, then there are probably a lot that should be done, but say which would require more intimate details of the class.

If it's local the local access point, then you are pretty much limited to range/reasonableness checks on the properties of the returned object. Anything beyond that, you aren't testing the method, your're testing the mocks.

Richard Lennox
06/10/2008 06:19 PM by
Richard Lennox

You must not forget the case where there are 0 webcasts or where the colection is null.

Alex Simkin
06/10/2008 06:40 PM by
Alex Simkin

Why PublishDate is nullable?

Scott Bellware
06/10/2008 06:45 PM by
Scott Bellware

Any attempts to answer this question that aren't phrased along the lines of "can you tell me more about the acceptance criteria" will be addressed by our human resources staff during your exit interview.

Andyk
06/10/2008 06:51 PM by
Andyk

I agree David, always 42!! ;)

Ayende Rahien
06/10/2008 06:59 PM by
Ayende Rahien

Alex,

Because you may want to have a webcast that is not published.

Scott,

LOL

Thomas Eyde
06/10/2008 07:22 PM by
Thomas Eyde

1, if you accept whitebox tests.

James Hicks
06/10/2008 09:34 PM by
James Hicks

More information is needed. What are the rules surrounding the publishing of web casts? Can more than one webcast be published on the same date? If so, do you rank them by their Id? How is a null PublishDate handled - is it in the future or in the past?

Peter Ritchie
06/11/2008 01:17 AM by
Peter Ritchie

Another vote for more information:

Does it throw exceptions? If so, what ones, and why?

Does it GetLatest() modify the state of the instance of the type it's contained in. If so, under what conditions?

Is a null a valid return value. If so, under what conditions will this occur?

...plus the other questions about more information.

Casey
06/11/2008 07:13 AM by
Casey

What is the current datetime ... because the "Latest" may well depend on that too....

firefly
06/12/2008 03:29 AM by
firefly

Scott got it :)

Comments have been closed on this topic.