I have written the following method using the Repository mentioned
in the following blog-post (http://www.codecapers.com/post/Using-RavenDB-with-ASPNET-MVC.aspx) using RavenDB:
public User GetUserById(string id)
{
var user = (from usr in _repository.All<User>() where usr.Id == id select usr).FirstOrDefault();
if (user == null)
{
throw new NullReferenceException("No user with the id (" + id + ") could be found.");
}
return user;
}
How would you unit test this method with nunit (and perhaps moq)?
“user” is just a normal class.
The first question is going to be – what are you testing in this context? The method provided really only has two outcomes, so you’re basically testing whether
useris null or not. Is that a value added test?As for the how, Im assuming
_repositoryis injected via some mechanism? If so, then you simply provide aMock<IRepository>(insert your type name as appropriate) and inject that in the place of_repositorywherever that is injected. Then you can setup the return values and test your method for the exception or not.