Reputation: 1236
Trying to get a basic understanding of unit testing. I've created a model class that contains a method that returns a "Person" object. Now i want to test if this method "GetPerson" is actually returning a Person object (P1).
Following the pattern of "Arrange, Act, Assert iv'e intanciated the Person class. I just don't know how to proceed from here. Would appreciate some help.
Person Class:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(int id, string firstName, string lastName)
{
Id = id;
FirstName = firstName;
LastName = lastName;
}
Person p1 = new Person(1, "John", "Dhoe");
public Person GetPerson()
{
return p1;
}
}
Test Class:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void GetPersonTest()
{
//Arrange
Person p = new Person(1, "John", "Dhoe");
//Act
//Assert
}
}
Upvotes: 0
Views: 3147
Reputation: 11
You can test for the successful creation of objects with:
var myPerson = new Person;
Assert.IsInstanceOf(myPerson, typeof(Person));
This is always a good first unit test for tesing classes.
Upvotes: 1
Reputation: 35901
This code is really strange (and will crash), but to fill the test for this case exactly:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void GetPersonTest()
{
//Arrange
Person p = new Person(0, "", ""); //note the change
//Act
Person result = p.GetPerson();
//Assert
Assert.AreEqual(1, result.Id);
Assert.AreEqual("John", result.FirstName);
Assert.AreEqual("Dhoe", result.LastName);
}
}
This doesn't make sense, because the GetPerson
method will return always the same person, regardless of what you pass to the constructor.
Also, as pointed out by Sriram Sakthivel, this code results in StackoverflowException
anyway :)
Still this a test for your GetPerson
method as its currently implemented.
Upvotes: 0