Locked Writing Fake/Mock Objects Manually

  • Friday, July 06, 2012 11:23 AM
     
     

     I am trying to learn Unit Testing. I have a method FreezeAllAccountsForUser as given below. It uses a AccountRepository to access data. How can I write unit tests for this method by manually mocking/faking?

    Note: I am using Visual Studio 2010 Team Test
    Note: I am new to unit testing.

    Note: FixedBankAccount and SavingsBankAccount are classes deriving from BankAccount class

         public void FreezeAllAccountsForUser(int userId)

        {
            List<DTOLayer.BankAccountDTOForStatus> bankAccountDTOList = new List<DTOLayer.BankAccountDTOForStatus>(); 

            IEnumerable<DBML_Project.BankAccount> accounts = AccountRepository.GetAllAccountsForUser(userId);
            foreach (DBML_Project.BankAccount acc in accounts)
            {
                string typeResult = Convert.ToString(acc.GetType());
                string baseValue = Convert.ToString(typeof(DBML_Project.BankAccount));

                if (String.Equals(typeResult, baseValue))
                {
                    throw new Exception("Not correct derived type");
                }

                acc.Freeze();

                DTOLayer.BankAccountDTOForStatus presentAccount = new DTOLayer.BankAccountDTOForStatus();
                presentAccount.BankAccountID = acc.BankAccountID;
                presentAccount.Status = acc.Status;
                bankAccountDTOList.Add(presentAccount);

            }



            IEnumerable<System.Xml.Linq.XElement> el = bankAccountDTOList.Select(x =>
                            new System.Xml.Linq.XElement("BankAccountDTOForStatus",
                              new System.Xml.Linq.XElement("BankAccountID", x.BankAccountID),
                              new System.Xml.Linq.XElement("Status", x.Status)
                            ));

            System.Xml.Linq.XElement root = new System.Xml.Linq.XElement("root", el);


            //AccountRepository.UpdateBankAccountUsingParseXML_SP(root);
            AccountRepository.Update();

        }



All Replies

  • Monday, July 09, 2012 6:35 AM
    Moderator
     
     Answered

    Hi Lijo,

    Thank you for posting in the MSDN forum.

    If you want to create a unit test for a class method in VS2010, you could open you class file, then right click the class method and select Create Unit Tests, then you will get the test method automatically, and then you would write your own code in it.

    This is a Walkthrough: Creating and Running Unit Tests, if possible, you could check it, you will get more information about the unit tests.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us