c# - How to use Moq to test a method with no return value? -


This is my first question, please be kind! :)

What I am trying to do is write a test for a manager class that during the construction many new examples of single item class are added in the list. When UpdateAllItems is called in this manager category, it is intended to iterate the intent list and make an incremental call to each individual item.

The manager class is my code, but the single item class is not the same, so I can not modify it.

I use NUnit for a test framework and I'm starting to work. . Because the manager class uses the single item class, I think I'll need to use a Moq, so I'm just testing the manager, not a single item.

How do I test a test for my UpdateAllItems method? (Technically I should know the first test).

Here is some sample code that gives me the general idea of ​​working with it ...

  public class single-id codecuntbofified {public int-current value {received ; Private set; } Public Single Entity Discounted Entitlement {CurrentValue = startValue; } Public Zero Enhance () {CurrentValue ++; }} Public Class Single Entrepreneur Manager {list & lt; Single IT CodecounterBmAffaed & gt; Item = new list & lt; Single item_codentbmodified & gt; (); Public SingleItemem Manager () {items.Add (new single item codecannedbymified (100)); item. Add (New SingleItem_CoderentBoamed (200)); } Public Zero UpdateAllItems () {items.ForEach (item => item.Increment ()); }}   

Thanks in advance for all help!

The simple answer is, you can not UpdateAllItems call ( Increment () ) method is non-virtual, so you will not be able to fake it.

Your options, as I see, are:

  • UpdateAllItems do not check at all. Its implementation is trivial, so it is an option to consider (though not ideal).
  • Make a real SingleItem_CodeCantBeModified example in your test Purists will say that you do not have the unit exam at this point, but this is still a useful test Could.
  • Add a ISingleItem interface, and a single itam adapter: icinglight code class which is in the context of SingleItem_CodeCantBeModified and calls Then you can work on ISingleItem s by typing single item manager , and you pass in your tests in the fake ISingleItem s To be free (depending on how your system is set up, you can descend from SingleItem_CodeCantBeModified , Applying the interface to your offspring, and instead of writing an adapter, you can use those items.)

    This last option gives you the most options, but some of the complexity at cost. Choose the option you are trying to complete.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -