I only use Setup, if almost all my test methods have common code. If not, I prefer to create a private method with the common code, and let the unit tests invoke it. e.g.
[TestFixture]
public class MailBoxTest(){
[Test]
public void PostOfficeExists(){
Assert.True(NJPostOffice().Exists());
}
[Test]
public void MailBoxEmpty(){
Assert.Equals(0, MailBoxAtNJPostOffice().Mails.Count);
}
[Test]
public void MailBoxWithMail(){
PostOffice postOffice = NJPostOffice();
postOffice.sendMailTo(222, new Mail(”Some Mail”));
Assert.True(MailBoxAtNJPostOffice().Mails.Count > 0);
}
private MailBox MailBoxAtNJPostOffice(){
MailBox mailBox = new MailBox(222, 07058);
}
private PostOffice NJPostOffice(){
PostOffice postOffice = new PostOffice(07058);
}
}
I could have put the code to create the NJPostOffice() in the Setup. However, not all methods require NJPostOffice() method to be executed. Therefore, I create a private method NJPostOffice() and let the methods needing it call it. It maintains the readability of the code also.
Tags: No Comments
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.