none
how to write nUnit for a line of code getting object by type

    Pertanyaan

  • Hi,

     I am new to writing nunit . below is the method for which I want to write nunit.

     public object GetValue()
            {
                int rev = 0;
    
                lock (IoC.Get<IResource>().SynchronizedObject)
                {
                    var revC = IoC.Get<IResource>().Concat(IoC.Get<IResource>().Removed)
                        .Where(n => n.Group == "Soft");
    
                    if (revC.Count() > 0)
                        rev = revC.Max(n => n.Revision);
                }
     
                return new { revision = Convert.ToString(rev) };
            }

    and below is my nunit code:

     public void GetValueTest()
            {
                SCClass SCObj = new SCClass ();
                object ActualResult = SCObj.GetValue();
                object ExptResult = new { revision = Convert.ToString(0) };
                Assert.AreSame(ActualResult, ExptResult);
            }

    But when GetValue method is called via nunit it is throwing exception "NotImplementedException - Get instance is not supported. Set _getInstance in bootstrapper if required." when it comes to the below line in method:

    IoC.Get<IResource>().SynchronizedObject

    But the Getvalue method works fine in normal debugging of method i.e without nunit.

    Thanks in advance.

    • Dipindahkan oleh CoolDadTx 11 Oktober 2018 14:47 Third party product
    11 Oktober 2018 9:56

Semua Balasan

  • Hi

    first of all I would use a AAA division to make it more readable:

    public void GetValueTest()
    {
        // Arrange
        SCClass sut = new SCClass ();
        object ExptResult = new { revision = Convert.ToString(0) 
        };
    
        // Act
        object ActualResult = sut.GetValue();
    
        // Assert
        Assert.AreSame(ExptResult, ActualResult);
    }
    I would name the object to be tested sut (System under test). Looks like ExptResult and ActualResult are reference compared.

    Greetings, Chris


    11 Oktober 2018 10:17
  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsunittest

    it is where you can post.

    11 Oktober 2018 10:31
  • Thanks.
    11 Oktober 2018 10:44
  • You can try this:

    public void GetValueTest()
    {
        // Arrange
        SCClass sut = new SCClass ();
        object ExptResult = new { revision = Convert.ToString(0) };
    
        // Act
        object ActualResult = sut.GetValue();
    
        // Assert
        Assert.AreEqual(ExptResult.revision, ActualResult.revision);
    }

    You just have one property in your object, so you could just check if it is equal.

    Greetings, Chris

    PS: Have you refered to the other project?
    11 Oktober 2018 10:44
  • Can you show us the whole code of the class SCClass?

    "NotImplementedException - Get instance is not supported. Set _getInstance in bootstrapper if required."

    Sounds like you have to do this before you run the test...

    Greetings, Chris

    11 Oktober 2018 14:45
  • Please post questions related to third party products like NUnit in their forums.

    Michael Taylor http://www.michaeltaylorp3.net

    11 Oktober 2018 14:46