简体   繁体   中英

Unit Testing stack based virtual machine c#

I'm currently working on a stack based virtual machine which loads commands from a text file and I need to test the class Increment and Decrement operations. I am fairly new to unit testing but I've worked on a few examples to get the hang of the syntax but now I've become a bit stuck!

When I try to run the tests I get a null value exception, so I guess my next question is would I be best to use Moq or Fakes to pass a value through to test to see if it passes the expected result?

I have had a look at the Fakes assembly and this is what I came up with but I'm not sure how I would pass a System.Collections

After this I have become stuck, any help / constructive help would be excellent.

EDIT- The integer value has been pushed to the stack prior to calling this class.

Sounds like you might be getting a little friction because the problem has been modelled slightly strangely. For my money, I'd avoid the difficulty of mocking by re-arranging your problem. For example, if you pass the stack into the operation itself, you'll get a much more natural test;

var stack = new Stack();
var op = new Decr();
stack.push(0);
op.Execute(stack);
Assert.AreEqual(-1, stack.Peek());

You'll have avoided unnecessary state on the Decr class. This is an example of the Dependency Inversion Principle, where dependent objects are passed into a class, rather than being created by it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM