locked
nunit test RRS feed

  • Question

  • User-1232255770 posted
    [Test]
        public void CreateUniqueAccount()
        {
          //Arange
          Account account = Account
    	{
    	 FirstName = Duncan
             LastName = Jones
    	}
              //Act
          Account accountCreated = Manager.CreateUniqueAccount(account);
    
          //Assert
          Assert.IsNotNull(accountCreated);
        
        }

    I would like to test the CreateUniqueAccount function, its creates an account in active directory.
    But for unit test  this is not desired. 
    So how to test without actually create an account, is the  mock library an option (using Moq;)?

    And what would the syntax look like to moq the account object ?

    Account account = new Mock<Account>  how to do the properties ?

    Monday, May 3, 2021 9:11 AM

Answers

  • User-474980206 posted

    You don’t mock Account, you mock the AD library CreateUniqueAccount calls. This means your Manager class should have been designed to be testable. The AD library it calls should be an interface that is replaceable. A simple way is via the constructor. 

     

    var adUtility = new mockAdUtility();
    var manager = new Manager(adUtility);
    Account accountCreated = manager.CreateUniqueAccount(account);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 3, 2021 2:45 PM

All replies

  • User1120430333 posted

    Is unit testing something you are doing  on your own, or are you doing this as company work? 

    Monday, May 3, 2021 10:54 AM
  • User-1232255770 posted

    for my work, but i am stuck on this.

    Monday, May 3, 2021 1:16 PM
  • User-474980206 posted

    You don’t mock Account, you mock the AD library CreateUniqueAccount calls. This means your Manager class should have been designed to be testable. The AD library it calls should be an interface that is replaceable. A simple way is via the constructor. 

     

    var adUtility = new mockAdUtility();
    var manager = new Manager(adUtility);
    Account accountCreated = manager.CreateUniqueAccount(account);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 3, 2021 2:45 PM
  • User1120430333 posted

    Bradly

    for my work, but i am stuck on this.

    Did your company give you any UT training classes before they tossed you into the frying pan, becuase UT is not an easy subject to learn and mocking is not all that easy to pick up either? I have seen too many software unit tests that the developer flat out thought they knew how to write them, but they didn't know.

    ASP.NET Core is not a UT forum. At best, MS Q&A has a vs-unittest forum you can post to, and maybe,  they can help you concerning unit testing and also using a mocking framework like Moq.

    I had to take mandatory 40 hour classes in two companies each concerning UT along  with using Rhino Mocks and then Moq classes before I knew what was really happening with UT and how to UT. 

    If your company has not done this for you, then your company has short-sheeted you,  and they have done you no favors in helping you be a successful UT software developer.

    Monday, May 3, 2021 4:15 PM