Is there a way to test non-static methods with NUnit?How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeNUnit vs Visual Studio 2008's Test Projects for Unit Testing?What is the best way to iterate over a dictionary?How do you unit test private methods?Static readonly vs constHow do I run NUnit in debug mode from Visual Studio?How to mark a method as obsolete or deprecated?How should I group unit tests using C# and NUnit?Live Testing not working with NUnit

Is the specular reflection on a polished gold sphere white or gold in colour?

Justifying Affordable Bespoke Spaceships

Explicit song lyrics checker

Counterfeit checks were created for my account. How does this type of fraud work?

When Bnei Yisroel travelled in the midbar, what happened on Shabbos?

How many people are necessary to maintain modern civilisation?

Is there a difference between an NFC and RFID chip?

How could empty set be unique if it could be vacuously false

How much steel armor can you wear and still be able to swim?

I just entered the USA without passport control at Atlanta airport

Boss wants someone else to lead a project based on the idea I presented to him

Cut the gold chain

Helping ease my back pain by studying 13 hours everyday , even weekends

Is the continuity test limit resistance of a multimeter standard?

Find All Possible Unique Combinations of Letters in a Word

"Correct me if I'm wrong"

Should the party get XP for a monster they never attacked?

What was the flower of Empress Taytu?

Rejecting an offer after accepting it just 10 days from date of joining

Are there any individual aliens that have gained superpowers in the Marvel universe?

Why don't we have a weaning party like Avraham did?

Print one file per line using echo

Has a life raft ever been successfully deployed on a modern commercial flight?

In Mistborn, why can metal in people's bodies be affected by Allomancy sometimes?



Is there a way to test non-static methods with NUnit?


How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeNUnit vs Visual Studio 2008's Test Projects for Unit Testing?What is the best way to iterate over a dictionary?How do you unit test private methods?Static readonly vs constHow do I run NUnit in debug mode from Visual Studio?How to mark a method as obsolete or deprecated?How should I group unit tests using C# and NUnit?Live Testing not working with NUnit






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm working on incorporating some unit tests into an old C# poker game that I'm trying to refactor (for practice). I ran into some trouble when trying to test a non-static method and I couldn't find any solutions.



The class diagram for the related classes looks like this.



This is the code for the non-static method:



 /// <summary>
/// Loops through the collection stored in cardArray[] and sets each of the card objects' inplay property to false.
/// </summary>
public void ResetUsage()

for (int i = 0; i < cardArray.Length; i++)

// Loops through the cardArray and sets inplay property to false.
cardArray[i].Inplay = false;




The array is of type SuperCard and is defined in the CardSet class's constructor. This is how its called in the Main():



private static void RunPokerSession()
{
// Create our deck object
CardSet myDeck = new CardSet();

while (PokerSession.Balance != 0)
{
myDeck.ResetUsage();

// Retrieves the computer and player hands.
SuperCard[] computerHand = myDeck.GetCards(PokerSession.HandSize);
SuperCard[] playerHand = myDeck.GetCards(PokerSession.HandSize);


The unit test that I'm trying to code is checking to see whether the InPlay property was indeed set to false for every card in the deck.



My question is whether its possible to code this test using NUnit?










share|improve this question






















  • You can test any observable effect. If you can observe the change, either by being told via events or similar, or manually by checking each card afterwards, then yes, you can test this. The main issue to consider is dependencies, are you able to actually run the code so that you can observe it, or will it be waiting for players, UI events, etc?

    – Lasse Vågsæther Karlsen
    Mar 25 at 7:29











  • Thank you for the clarification. For some reason, I thought it wasn't possible to test non-static methods. I should mention that this is my first time experimenting with unit tests.

    – Asel S
    Mar 25 at 17:06

















0















I'm working on incorporating some unit tests into an old C# poker game that I'm trying to refactor (for practice). I ran into some trouble when trying to test a non-static method and I couldn't find any solutions.



The class diagram for the related classes looks like this.



This is the code for the non-static method:



 /// <summary>
/// Loops through the collection stored in cardArray[] and sets each of the card objects' inplay property to false.
/// </summary>
public void ResetUsage()

for (int i = 0; i < cardArray.Length; i++)

// Loops through the cardArray and sets inplay property to false.
cardArray[i].Inplay = false;




The array is of type SuperCard and is defined in the CardSet class's constructor. This is how its called in the Main():



private static void RunPokerSession()
{
// Create our deck object
CardSet myDeck = new CardSet();

while (PokerSession.Balance != 0)
{
myDeck.ResetUsage();

// Retrieves the computer and player hands.
SuperCard[] computerHand = myDeck.GetCards(PokerSession.HandSize);
SuperCard[] playerHand = myDeck.GetCards(PokerSession.HandSize);


The unit test that I'm trying to code is checking to see whether the InPlay property was indeed set to false for every card in the deck.



My question is whether its possible to code this test using NUnit?










share|improve this question






















  • You can test any observable effect. If you can observe the change, either by being told via events or similar, or manually by checking each card afterwards, then yes, you can test this. The main issue to consider is dependencies, are you able to actually run the code so that you can observe it, or will it be waiting for players, UI events, etc?

    – Lasse Vågsæther Karlsen
    Mar 25 at 7:29











  • Thank you for the clarification. For some reason, I thought it wasn't possible to test non-static methods. I should mention that this is my first time experimenting with unit tests.

    – Asel S
    Mar 25 at 17:06













0












0








0








I'm working on incorporating some unit tests into an old C# poker game that I'm trying to refactor (for practice). I ran into some trouble when trying to test a non-static method and I couldn't find any solutions.



The class diagram for the related classes looks like this.



This is the code for the non-static method:



 /// <summary>
/// Loops through the collection stored in cardArray[] and sets each of the card objects' inplay property to false.
/// </summary>
public void ResetUsage()

for (int i = 0; i < cardArray.Length; i++)

// Loops through the cardArray and sets inplay property to false.
cardArray[i].Inplay = false;




The array is of type SuperCard and is defined in the CardSet class's constructor. This is how its called in the Main():



private static void RunPokerSession()
{
// Create our deck object
CardSet myDeck = new CardSet();

while (PokerSession.Balance != 0)
{
myDeck.ResetUsage();

// Retrieves the computer and player hands.
SuperCard[] computerHand = myDeck.GetCards(PokerSession.HandSize);
SuperCard[] playerHand = myDeck.GetCards(PokerSession.HandSize);


The unit test that I'm trying to code is checking to see whether the InPlay property was indeed set to false for every card in the deck.



My question is whether its possible to code this test using NUnit?










share|improve this question














I'm working on incorporating some unit tests into an old C# poker game that I'm trying to refactor (for practice). I ran into some trouble when trying to test a non-static method and I couldn't find any solutions.



The class diagram for the related classes looks like this.



This is the code for the non-static method:



 /// <summary>
/// Loops through the collection stored in cardArray[] and sets each of the card objects' inplay property to false.
/// </summary>
public void ResetUsage()

for (int i = 0; i < cardArray.Length; i++)

// Loops through the cardArray and sets inplay property to false.
cardArray[i].Inplay = false;




The array is of type SuperCard and is defined in the CardSet class's constructor. This is how its called in the Main():



private static void RunPokerSession()
{
// Create our deck object
CardSet myDeck = new CardSet();

while (PokerSession.Balance != 0)
{
myDeck.ResetUsage();

// Retrieves the computer and player hands.
SuperCard[] computerHand = myDeck.GetCards(PokerSession.HandSize);
SuperCard[] playerHand = myDeck.GetCards(PokerSession.HandSize);


The unit test that I'm trying to code is checking to see whether the InPlay property was indeed set to false for every card in the deck.



My question is whether its possible to code this test using NUnit?







c# .net unit-testing nunit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 6:50









Asel SAsel S

5018




5018












  • You can test any observable effect. If you can observe the change, either by being told via events or similar, or manually by checking each card afterwards, then yes, you can test this. The main issue to consider is dependencies, are you able to actually run the code so that you can observe it, or will it be waiting for players, UI events, etc?

    – Lasse Vågsæther Karlsen
    Mar 25 at 7:29











  • Thank you for the clarification. For some reason, I thought it wasn't possible to test non-static methods. I should mention that this is my first time experimenting with unit tests.

    – Asel S
    Mar 25 at 17:06

















  • You can test any observable effect. If you can observe the change, either by being told via events or similar, or manually by checking each card afterwards, then yes, you can test this. The main issue to consider is dependencies, are you able to actually run the code so that you can observe it, or will it be waiting for players, UI events, etc?

    – Lasse Vågsæther Karlsen
    Mar 25 at 7:29











  • Thank you for the clarification. For some reason, I thought it wasn't possible to test non-static methods. I should mention that this is my first time experimenting with unit tests.

    – Asel S
    Mar 25 at 17:06
















You can test any observable effect. If you can observe the change, either by being told via events or similar, or manually by checking each card afterwards, then yes, you can test this. The main issue to consider is dependencies, are you able to actually run the code so that you can observe it, or will it be waiting for players, UI events, etc?

– Lasse Vågsæther Karlsen
Mar 25 at 7:29





You can test any observable effect. If you can observe the change, either by being told via events or similar, or manually by checking each card afterwards, then yes, you can test this. The main issue to consider is dependencies, are you able to actually run the code so that you can observe it, or will it be waiting for players, UI events, etc?

– Lasse Vågsæther Karlsen
Mar 25 at 7:29













Thank you for the clarification. For some reason, I thought it wasn't possible to test non-static methods. I should mention that this is my first time experimenting with unit tests.

– Asel S
Mar 25 at 17:06





Thank you for the clarification. For some reason, I thought it wasn't possible to test non-static methods. I should mention that this is my first time experimenting with unit tests.

– Asel S
Mar 25 at 17:06












2 Answers
2






active

oldest

votes


















0














Yes, it is possible.



If you can instantiate the CardSet class in a junit test you can test that ResetUsage() does what you expect. If you can observe what RunPokerSession does to a CardSet will depend on the design and structure of RunPokerSession, and that is not clear from you question. You will, most likely, want to inject a CarSet to the RunPokerSession and then observe the result. Can you do that with the current implementation?






share|improve this answer























  • Yes, I was able to do so. I will post my solution here.

    – Asel S
    Mar 25 at 17:08











  • Great! Good for you!

    – Jocke
    Mar 26 at 7:32


















0














With Lasse's and Jocke's help, I was able to think through the implementation. This is how I implemented the unit test for my non-static method:



 [Test]
public void ResetUsage_ValidateInPlayReset_ReturnsTrue()

CardSet testdeck = new CardSet();

testdeck.ResetUsage();

bool result = false;

for (int i = 0; i < testdeck.cardArray.Length; i++)

if (testdeck.cardArray[i].Inplay == false)

result = true;



Assert.That(result = true);






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332516%2fis-there-a-way-to-test-non-static-methods-with-nunit%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Yes, it is possible.



    If you can instantiate the CardSet class in a junit test you can test that ResetUsage() does what you expect. If you can observe what RunPokerSession does to a CardSet will depend on the design and structure of RunPokerSession, and that is not clear from you question. You will, most likely, want to inject a CarSet to the RunPokerSession and then observe the result. Can you do that with the current implementation?






    share|improve this answer























    • Yes, I was able to do so. I will post my solution here.

      – Asel S
      Mar 25 at 17:08











    • Great! Good for you!

      – Jocke
      Mar 26 at 7:32















    0














    Yes, it is possible.



    If you can instantiate the CardSet class in a junit test you can test that ResetUsage() does what you expect. If you can observe what RunPokerSession does to a CardSet will depend on the design and structure of RunPokerSession, and that is not clear from you question. You will, most likely, want to inject a CarSet to the RunPokerSession and then observe the result. Can you do that with the current implementation?






    share|improve this answer























    • Yes, I was able to do so. I will post my solution here.

      – Asel S
      Mar 25 at 17:08











    • Great! Good for you!

      – Jocke
      Mar 26 at 7:32













    0












    0








    0







    Yes, it is possible.



    If you can instantiate the CardSet class in a junit test you can test that ResetUsage() does what you expect. If you can observe what RunPokerSession does to a CardSet will depend on the design and structure of RunPokerSession, and that is not clear from you question. You will, most likely, want to inject a CarSet to the RunPokerSession and then observe the result. Can you do that with the current implementation?






    share|improve this answer













    Yes, it is possible.



    If you can instantiate the CardSet class in a junit test you can test that ResetUsage() does what you expect. If you can observe what RunPokerSession does to a CardSet will depend on the design and structure of RunPokerSession, and that is not clear from you question. You will, most likely, want to inject a CarSet to the RunPokerSession and then observe the result. Can you do that with the current implementation?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 25 at 10:40









    JockeJocke

    1,73911219




    1,73911219












    • Yes, I was able to do so. I will post my solution here.

      – Asel S
      Mar 25 at 17:08











    • Great! Good for you!

      – Jocke
      Mar 26 at 7:32

















    • Yes, I was able to do so. I will post my solution here.

      – Asel S
      Mar 25 at 17:08











    • Great! Good for you!

      – Jocke
      Mar 26 at 7:32
















    Yes, I was able to do so. I will post my solution here.

    – Asel S
    Mar 25 at 17:08





    Yes, I was able to do so. I will post my solution here.

    – Asel S
    Mar 25 at 17:08













    Great! Good for you!

    – Jocke
    Mar 26 at 7:32





    Great! Good for you!

    – Jocke
    Mar 26 at 7:32













    0














    With Lasse's and Jocke's help, I was able to think through the implementation. This is how I implemented the unit test for my non-static method:



     [Test]
    public void ResetUsage_ValidateInPlayReset_ReturnsTrue()

    CardSet testdeck = new CardSet();

    testdeck.ResetUsage();

    bool result = false;

    for (int i = 0; i < testdeck.cardArray.Length; i++)

    if (testdeck.cardArray[i].Inplay == false)

    result = true;



    Assert.That(result = true);






    share|improve this answer



























      0














      With Lasse's and Jocke's help, I was able to think through the implementation. This is how I implemented the unit test for my non-static method:



       [Test]
      public void ResetUsage_ValidateInPlayReset_ReturnsTrue()

      CardSet testdeck = new CardSet();

      testdeck.ResetUsage();

      bool result = false;

      for (int i = 0; i < testdeck.cardArray.Length; i++)

      if (testdeck.cardArray[i].Inplay == false)

      result = true;



      Assert.That(result = true);






      share|improve this answer

























        0












        0








        0







        With Lasse's and Jocke's help, I was able to think through the implementation. This is how I implemented the unit test for my non-static method:



         [Test]
        public void ResetUsage_ValidateInPlayReset_ReturnsTrue()

        CardSet testdeck = new CardSet();

        testdeck.ResetUsage();

        bool result = false;

        for (int i = 0; i < testdeck.cardArray.Length; i++)

        if (testdeck.cardArray[i].Inplay == false)

        result = true;



        Assert.That(result = true);






        share|improve this answer













        With Lasse's and Jocke's help, I was able to think through the implementation. This is how I implemented the unit test for my non-static method:



         [Test]
        public void ResetUsage_ValidateInPlayReset_ReturnsTrue()

        CardSet testdeck = new CardSet();

        testdeck.ResetUsage();

        bool result = false;

        for (int i = 0; i < testdeck.cardArray.Length; i++)

        if (testdeck.cardArray[i].Inplay == false)

        result = true;



        Assert.That(result = true);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 17:11









        Asel SAsel S

        5018




        5018



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332516%2fis-there-a-way-to-test-non-static-methods-with-nunit%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript