Moq&n;复杂方法

时间:2014-07-28 14:37:09

标签: unit-testing moq crm

我继承了一堆代码库,并且我被要求尝试进行有效的“单元测试”和自动化测试,以支持即将发生的变化。我们正在使用Crm 4.0。以下是我尝试使用Moq的代码示例:

public static creditdebitcardEntity CreateCreditCardInstance(Transaction transaction, TransactionSet transactionSet, accountEntity stetyAccount)
    {
        try
        {
            //todo: check for existance
            creditdebitcardEntity stetyCard;
            ColumnSet customColumns = new ColumnSet();
            customColumns.Attributes = new string[]{creditdebitcardEntity.creditdebitcardidName,
                                                    creditdebitcardEntity.expirationmonthName,
                                                    creditdebitcardEntity.expirationyearName,
                                                    creditdebitcardEntity.reasonheldName,
                                                    creditdebitcardEntity.heldonName,
                                                    creditdebitcardEntity.statuscodeName};
            creditdebitcardEntity[] tempCard =
                creditdebitcardDal.GetCreditCardEntityByHashcode(transaction.CreditCards.CreditCardHash, customColumns);

            if (tempCard != null)
                return tempcard;
            else
                return null;
        }
        catch (Exception ex)
        {
            DirectiveExceptions.ReturnStagingException("CreditCardDirectives", "CreateCreditCardInstance", "An error occurred when trying to create a credit card transaction instance for folio " + transaction.SourceTransactionID + ". Contact a system administrator", ex, transaction.SourceTransactionKey);
            return null;
        }
    }

所以我的问题是这个;如何模拟对CreditDebitCardDal的调用?我不能在不必经过一堆小门的情况下对代码库进行更改。我可以毫不费力地引入新代码,但修改现有代码需要进行大量的回归测试。

我确实理解如何模拟对静态方法的调用,并且传入所需的对象也没有问题。我只是不想依赖我的SUT有一个Crm数据库来确保代码正常工作。我想我可以创建一个Dal类的接口然后模拟那个接口,但是想在我开始走这条路之前看看是否有更好的方法。

谢谢,如果我能提供任何其他详细信息,请告诉我。 卡盘

1 个答案:

答案 0 :(得分:0)

我会嘲笑开始的参数。从Transaction,TransactionSet,accountEntity中提取接口。然后,您可以模拟出依赖关系。你还有更多的工作要做,但应该遵循以下几点。下面的代码只是为了向您展示,它未经过测试或编译

[TestFixture]
public class CreditDebitCardUnitTests
{

   private Mock<ITranscation> MockTransaction {get ; set;}
   private Mock<ITransactionSet> MockTransactionSet {get; set;}
   private Mock<IAccountEntity> MockAccountEntity {get; set;}

   [Setup]
    public void TestFixtureSetup()
    {
         MockTransaction = new Mock<ITransaction>();
         MockTransactionSet = new Mock<ITransactionSet>();
         MockAccountEntity  = new Mock<IAccountEntity>();



    }

   [Test]
   [Category("CreditCard tets")]
   public void VerfiyCreditDebitCardDal_GetCreditCardEntityByHashcode_Gets_Called
   {
      //Arrange
      ColumnSet customColumns = new ColumnSet();
        customColumns.Attributes = new string[]{creditdebitcardEntity.creditdebitcardidName,
                                                creditdebitcardEntity.expirationmonthName,
                                                creditdebitcardEntity.expirationyearName,
                                                creditdebitcardEntity.reasonheldName,
                                                creditdebitcardEntity.heldonName,
                                                creditdebitcardEntity.statuscodeName};

       MockTransaction.Setup(x => x.CreditCards.CreditCardHash).Returns(It.IsAny<Hash>)
      //Act
      creditdebitcardEntity[] tempCard =
            creditdebitcardDal.GetCreditCardEntityByHashcode(MockTransaction.ObjectCreditCards.CreditCardHash, customColumns  )
      //Assert

   }
}