JustMock: Returning null in unit tests

Sometimes it is necessary to return null inside a unit test. JustMock has its own style doing this. Take a look at the following method:


public string GetUsername(Guid userId)
{
  User user = UserRepository.GetUser(userId);
  string username = string.Empty;
  if (user != null)
    username = user.Username;
  return username;
}

If you want to test that the method returns an empty string if no user is found inside your repository, you have to arrange that your mocked repository returns null.

The testmethod should look like this:


[TestMethod]
public void GetUsername_Returns_Empty_String_If_User_Not_Found()
{
  IUserRepository userRepository = Mock.Create<IUserRepository>();
  Guid userId = Guid.NewGuid();
  Mock.Arrange(() => userRepository.GetUser(userId)).Returns(null);

  UserManager userManager = new UserManager(userRepository);
  string username = userManager.GetUsername(userId);
	 
  Assert.IsTrue(username.Equals(string.Empty));
}

The code won’t compile, because the line


Mock.Arrange(() => userRepository.GetUser(userId)).Returns(null);

throws an error.

The correct way would be


Mock.Arrange(() => userRepository.GetUser(userId)).Returns((User)null);

– or –


Mock.Arrange(() => userRepository.GetUser(userId)).Returns(() => null);

Subversion: Restoring db/current

Today I had a strange issue when I was trying to update a working copy of my Subversion repository. The /db/current file was missing for some reason and therefore any subversion related operation was not possible. After querying Google I found a blog post, that offered a very simple solution without restoring from a backup:

svnadmin recover <PathToRepo>

The command took a while, but when it finished the current file was recreated and my repository was intact. I have to mention that this is only working for repositories with version 1.5 and above.

Der angegebene Assemblyname oder die Codebasis ist ungültig. (Ausnahme von HRESULT: 0x80131047)

Der Fehler tritt auf, wenn man versucht per Reflection einen invaliden Typ zu ermitteln. Beispiel:


string typename = "SampleLib.Sample, SampleLib, Version=1.0.0.0,Culture=neutral;PublicKeyToken=1e6becac76b64446";
Type type = Type.GetType(typename);

Der Ursprung des Fehlers liegt hier in einem Tippfehler innerhalb des typname-Strings (; statt ,). Hat mich wertvolle Zeit gekostet, bis ich das gesehen hatte…