mardi 13 septembre 2016

Logic of calling a method to be tested in TDD

As I am new in tdd development, I tried mocking for the first time and I have a doubt regarding the same.

Consider following example:

here goes Business

public class MyEmail
{
    public bool SendEmail()
    {
           Email code goes here 
           return true;
    }
}

Another class 

public class Customer

{
  public bool Addcustomer()
  {
     MyEmail obj = new MyEmail();
     obj.SendEmail();
     return true;
  }
}

Here goes my test for Addcustomer

[TestMethod()]
public void AddCustomerTest()
{
  Mock<Myemail> objemail = new Mock<Myemail>();

  objemail.Setup(x=>x.SendEmail()).Returns(true);

customer obj = new Customer();// Created object of customer class because I want to test the methos that contains in this class.

Now My question Lies here??

 obj.AddCustomer(objemail.Object);

Please check above statement. We have called a method in class Customer with a parameter but actually that method does not accepts any param. But that method is not receiving any compilation error or run time error. How is it possible?

Is it only happening in test methods because ideally this should not be acceptable by compiler as per c# basics.

and moreover suppose if my Addcustomer method in class Customer is already having a defination like below. Then what should I do???

  public bool Addcustomer(int Anynumber)
  {
     MyEmail obj = new MyEmail();
     obj.SendEmail(Anynumber));
     return true;
  }

  Assert.AreEqual(obj.AddCustomer(), true);
}

Aucun commentaire:

Enregistrer un commentaire