Geeks With Blogs
Abhishek Anand Bits & Bytes about .net technology framework. unit testing
Testing Workflow Events
Imagine you are testing a method that returns or processes a Workflow event. Normally, you would generate a fake instance of the class and insert it into the method using Moq. However, creating a fake Workflow event is difficult, as Workflow events do not expose any public constructors. You can generate Workflow events using Microsoft.Activities.UnitTe... Note: this is a package that must be installed. Here is an example of generating “WorkflowApplicationComplet... Basically, run a workflow ......

Posted On Thursday, November 21, 2013 12:12 PM

Unit tests for HttpClient using HttpMessageHandler
You can unit test a class that uses HttpClient by giving that HttpClient a mock HttpMessageHandler. This way, you can capture the request and prevent it from actually going over the wire. Here is an example using Moq. HttpClient depends on HttpMessageHandler’s SendAsync() method, so give SendAsync() a stub implementation and use Moq’s Callback() to capture arguments. var handler = new Mock<HttpMessageHandler&... handler.Protected() .Setup<Task<HttpRespo... ......

Posted On Wednesday, November 20, 2013 4:32 PM

Unit Testing Non-Public Methods
Before we proceed any further, I should say this, Testing through this public API should be your first choice.Alternative for Abstract Base Classes One common case is when an abstract base class defines functionality in some protected methods, and there are a series of inheriting classes using those methods without overriding them. In this case, rather than choosing one inheriting member at random to test the methods, you can simply create a “Fake” class locally in your test project that inherits ......

Posted On Wednesday, November 20, 2013 4:03 PM

Assembly Initialize
AssemblyInitialize is very similar to the ClassInitialize and TestInitialize attributes, but rather than executing before classes/methods are run it executes before the any method marked with TestMethod, ClassInitialize, or TestInitialize inside this assembly. Only one method in an assembly can be annotated with this attribute. Code that belongs in the method decorated with AssemblyInitialize is any code that sets up shared resources that are used by tests across the assembly. Tests should not change ......

Posted On Monday, November 18, 2013 7:13 PM

Unit Testing Abstract Class and Protected Methods
Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 Unit Testing Protected Methods You are responsible for unit testing your Protected methods just like the Public ones. The suggested guidance for how to do so is to create a fake class in your unit test that inherits from the class you are testing. Then in this new class, create public methods that expose the protected members of the base class. You can now use this fake child class in your tests.Unit Testing Abstract Classes ......

Posted On Monday, October 21, 2013 1:47 PM

Moq – Return a Parameter
Moq provides an additional overload for the Returns method that takes a lambda expression with the same parameters as the method signature. When this overload is used, Moq will inject the parameters passed to the Setup method into the Returns as its lambda expression parameters. Note: The parameters for the lambda expression must match up in both number and ordering to the method signature. Example 1: public interface ITestInterface { string SomeMethod(string str); } … mockTestInterface.Setup(i => ......

Posted On Monday, November 18, 2013 6:27 PM

Test Initialize vs. Class Initialize
One common oversight we have encountered while working with developers is the mix up of the [TestInitialize] and [ClassInitialize] Attributes. When using these methods, be conscience of their differences. The Class Initialize attribute executes the decorated method once before the first test in the class is run, while the Test Initialize attribute executes before each test is run. There are different scenarios for when to use each, and putting code incorrectly in one or the other can cause unexpected ......

Posted On Monday, November 18, 2013 6:10 PM

Copyright © Abhishek Anand | Powered by: GeeksWithBlogs.net