Write test cases for dynamic pages by RapidWebDev.Mocks

The RapidWebDev UI Mock Framework aims to help developers test their business logic code which developed in dynamic page.

How to use Mock framework to write Unit Test for Dynamic Page?

So far every web page develops in RapidWebDev has to inherit one of interfaces (IDynamicPage, IDetailPanelPage, and IAggregatePanelPage).
The Mock framework also provides three proxy classes to map these interfaces, which are:

Wrap business class into Proxy class

Before you start writing your testcase, you need to know the classes which need to be tested inherit from which interfaces, then choose the mapping proxy class, and use the constructor of the proxy class to wrap your business class.
public DetailPanelPageProxy (object actual)
    this.actual = actual;
For example: (Test the Create () method which in UserDetailPanel)
There is a web page names UserDetailPanel which inherit from IDynamicPage
public class UserDetailPanel : DetailPanelPage
public abstract class DetailPanelPage : IDetailPanelPage
So in you testcase, the first step is create your business class, create corresponding proxy class
UserDetailPanel page = new UserDetailPanel();
DetailPanelPageProxy proxy = new DetailPanelPageProxy(page);

Simulate Http Running Environment

In order to simulate the actual web event and pass the necessary Get/Post parameters, here provides the class to simulate Http Running Environment, which names HttpEnvironment, it provides several methods to help develop set parameters
void SetRequestUrl(string uri)
void SetFormParameters(string key, string value)
void SetSessionParaemeter(string key, object value)
After set values, Developer can get values of parameters from HttpContext/IRequestHandler
For Example (Continue with the first example):
UserDetailPanel need to get the value of “Domain”, so you can use SetRequestUrl() to set it, and use HttpContext.Current.“Domain”/requestHandler.Parameters"Domain" to get it
using (var httpEnv = new HttpEnvironment())

Get/Set Web Controls belong to Dynamic Page

Since Developer needs to get/set the value of web control by himself, the proxy class also provides the methods to let developer Get/Set web control which in this dynamic page.
virtual void Set(string controlKey, Control controlValue)
virtual object Get(string controlKey)
TextBox TextBoxUserName = new TextBox();
TextBoxUserName.Text = "Eunge" + surfix;
proxy.Set("TextBoxUserName", TextBoxUserName);// Set TextBox to UserDetailPanel

Call the method which want to be tested

After setup all things we describe above, Developer can call the method which to be tested on proxy class
 string entity = proxy.Create();

Verify the result

Developer can use any assertion that provides by other frameworks such as NUnit, etc. here we take NUnit as example
UserObject user = membershipApi. Get (new Guid(entityId));
Assert.AreEqual(user. UserName, "testUser"+surfix);

What the limitation are

Developer must setup Http Running Environment, provides the necessary parameters, and bind web controls before call the method on proxy class.
Right now, only can test the methods the in the interfaces(IDynamicPage, IDetailPanelPage, and IAggregatePanelPage), doesn't support user-defined methods

Last edited Feb 12, 2010 at 9:03 AM by eungeliu, version 3


No comments yet.