When to use unit testing mock objects ?
Usually when to write unit test, you are trying to test if a small piece of code is working as intended. But there are some times when part of that small code depends upon third party web service or database connection that are simply out of your control.Thats when you use mock objects which would behave exactly as you tell them.
Following is simple code example that would explain it further:
class DataManager {
private $exception_logger;
public function __construct(IExceptionLogger exception_logger){
$this->exception_logger = $exception_logger;
}
public function find_user_by_email($email){
try{
$user_data_store = new UserDataStore();
$user = $user_data_store->find_by_email($email);
return $user;
}catch(Exception $ex){
$this->exception_logger->log($ex);
}
}
}
To test above code you could write unit test that would fetch user record based on email address. But how would you unit test if user data store object throws exception?
In that case change your code as following :
class DataManager
{
private $user_data_store;
private $exception_logger;
public function __construct(IUserDataStore $user_data_store,IExceptionLogger exception_logger){
$this->user_data_store = $user_data_store;
$this->exception_logger = $exception_logger;
}
public function find_user_by_email($email){
try{
$user = $this->user_data_store->find_by_email($email);
return $user;
}catch(Exception $ex) {
$this->exception_logger->log($ex);
}
}
}
Now in your unit test. Pass “Mocked” user data store class which would behave exactly as you specify. In this case it would throw an exception when call is made to its find_by_email() method.
This way you can write unit test if your exception logger class gets called and exception is recorded by logging class which wouldn’t have been possible in previous case.
I hope this clears concept of mocking in unit testing
How to use Subversion , nant & cruise - go build server to achieve continuous integration of .net projects
Use subversion as source code repository. All working code goes inside /trunk folder. Set Cruise- Go server to continuously poll subversion server for modification and if it senses change, then cruise agent pull all the code. You can do this through setting pipeline in cruise configuration file.
NANT build script themselves are part of source code so they gets pulled in whenever cruise agent download source code. It means build script themselves are version controlled ;)
Build scripts then run tasks as specified in cruise configuration files (pipeline configuration), usually compile project , run unit tests , copy correct configuration files based on environment name parameter (dev , qa , live) and then copy them to target folder.
QA server disk must be accessible to cruise agents - thus it can push latest changes & configuration files as soon as source code is changed on svn. These folders on QA server are also mapped to IIS web sites.
This way QA websites are continuously updated with code changes, asset files, configuration files.
Heres diagram :

Facebook timeline -This is how your facebook timeline is going to look like
How to use C# inside nant build scripts
I use c# to take input from user (via command line) to take certain actions in build script. Here’s code that ask name of web app and will copy necessery files to target directories.
<target name=”AskAndDeploy”>
<script language=”C#” prefix=”DNSNamePrefix” >
<code><![CDATA[
[Function(“ask”)]
public string AskDNSNamePrefix(string prompt) {
Project.Log(Level.Info, prompt);
try
{
return Console.ReadLine();
}
finally
{
}
}
]]></code>
</script>
<echo message=”If Your website is called foo.dev.co.uk then enter foo as DNS Prefix. Config files will be updated to point AT http://foo.dev.co.uk ” />
<property name=”DNSNamePrefix” value=”${DNSNamePrefix::ask(‘What is the web DNS name prefix ?’)}” />
<property name=”Build.Env.Name” value=”qa” />
<property name=”Solution.Dir.Path” value=”C:\Projects\phoenix” />
<copy todir=”\appqawin\projects\${DNSNamePrefix}WS.qa">
<fileset basedir=”${Solution.Dir.Path}\src\JB.WebService">
<include name=”**\*”/>
<exclude name=”**/bin/JB.Common.config” />
</fileset>
</copy>
</target>
How to use continuous integration for asp.net projects
How i use continuous integration server for automatic & faster delivery of .net projects
What you need :
1> Go (Cruise) continuous integration server
2> Go Agents - these are programs that actually run your tasks
3> SVN - Version control system
4> NUNIT - Unit testing framework
5> NANT - Build scripts for Deployments
Set up Visual Studio projects using TreeSurgeon. It nicely lays out basic structure for you which can further expanded. I try to keep Business logic, web app in separate sub projects - just to make life easier.
It usually looks like this
- /build —> used for holding build files or any other tmp files
- /configs —> configuration files
- /docs
- /libs —> globally used libraries
- /sql —> db scripts & schema’s
- /src —> main visual studio project source code
- /tools —>nant , nunit files
- go.bat —> batch file that starts build scripts via command line (@tools\nant\NAnt.exe -buildfile:web.build %*)
- web.build —> nant build file
Everything is stored in subversion. But any thing that can be generated using compiling stays out i.e. web project dll’s , bin folder , obj folder etc.
As soon as project is checked in. Go server + Go agents starts downloading latest /trunk of project and start executing build file using following command
<stage name=”qa”>
<jobs>
<job name=”deploy”>
<resources>
<resource>windows</resource>
</resources>
<tasks>
<exec command=”go.bat qa” />
</tasks>
</job>
</jobs>
</stage>
This simply starts go.bat file with name of environment i.e. qa . Based on environment name build script picks up correct versions of configuration file (QA , staging , live).It also runs unit tests.
If all unit testes are passed then it would deploy asp.net project in designated directory on web servers directories along with correct version of QA configuration files.
What do you think about this process?
Drop me a mail.
