chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I am new at unit testing:
I have a pretty simple class, below (mysite.cs).
public class MySite : IComparable
{
[XmlAttribute]
public Guid Id { get; set; }

[XmlAttribute]
public string Url { get; set; }

[XmlAttribute]
public string Title { get; set; }

override public string ToString()
{
return Title;
}

Following is my test method, which runs successfully when I would expect it to fail, given the title is different then the expectation of the tostring method, even though the overridden tostirng method just returns the title property.

[Isolated]
[Test]
public void TestMethod1()
{
//Arrange
MySite fake = Isolate.Fake.Instance<MySite>(Members.ReturnRecursiveFakes);
fake.Title = "123";
Isolate.WhenCalled(() => fake.ToString()).WillReturn("Test 123");

//Act
fake.ToString();

//Assert
Isolate.Verify.WasCalledWithAnyArguments(() => fake.ToString());
}
asked by Sorbitol (3.2k points)

4 Answers

0 votes
Hi Sorbitol,
What you are testing (Isolate.Verify) is that fake.ToString() has been called. It obviously has in the previous statement.
You might want to check the return value.

[Isolated,Test]
public void TestMethod1()
{
  //Arrange
  MySite fake = Isolate.Fake.Instance<MySite>(Members.ReturnRecursiveFakes);
  Isolate.WhenCalled(() => fake.ToString()).WillReturn("Test 123");

  //Act
  var result = fake.ToString();

  //Assert
  Assert.AreEqual("Test 123", result);
}
answered by scott (32k points)
0 votes
So, here is what I did based on your suggestions and it worked great!

My next question that I could not find in the documentation is how to verify that a particular property is returned when calling a method: Basically, when calling the tostring method, the property title was returned. My attempt is wrong:

//Arrange
MySite fake = Isolate.Fake.Instance<MySite>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fake.Title).ReturnRecursiveFake();
Isolate.WhenCalled(() => fake.ToString()).ReturnRecursiveFake();

//Act
fake.ToString();

//Assert
Isolate.Verify.WasCalledWithAnyArguments(() => fake.Title);
answered by Sorbitol (3.2k points)
0 votes
So, I think I figured out the right syntax, but looking for confirmation;
I want to test that when I call the public tostring method, the Title property is called:
[Isolated, Test]
public void TestCallingToStringCallsTitleProperty()
{
//Arrange
MySite fake = Isolate.Fake.Instance<MySite>(Members.CallOriginal);
Isolate.WhenCalled(() => fake.Title).ReturnRecursiveFake();

//Act
fake.ToString();

//Assert
Isolate.Verify.WasCalledWithAnyArguments(() => fake.Title);
}
answered by Sorbitol (3.2k points)
0 votes
Hi,

Yes that will work an alternative way is to take advantage of the fact that once you fake a class all methods by default will return recursive fake. So in your test you only need to call the original implementation of ToString.
//Arrange
MySite fake = Isolate.Fake.Instance<MySite>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fake.ToString()).CallOriginal();

//Act
fake.ToString();

//Assert
Isolate.Verify.WasCalledWithAnyArguments(() => fake.Title);

answered by ohad (35.5k points)
...