Translate

Tuesday, August 9, 2011

Implementing Rhino Mocks Using Visual Studio with Repository and Service Test Cases

Implementing Rhino Mocks Using Visual Studio with Repository


and Service Test Cases



This is an Example used for to explain about how to create Repository and Service Test cases using Rhino Mocks




Common Objects

EmployeeFilter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UniversityEmployeeInformation.CommonObjects
{
public class EmployeeFilter
{
public int EmployeeId { get; set; }
public string EmployeeDept { get; set; }
}
}

MessageException



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;


namespace UniversityEmployeeInformation.CommonObjects
{

public class MessageException : Exception
{
public MessageException(string message)
: base(message)
{

}

}


}

ServiceFault


using System.Runtime.Serialization;


namespace UniversityEmployeeInformation.CommonObjects
{
[DataContract]
public class ServiceFault
{
[DataMember]
public MessageException FaultException { get

; set; }




}
}

UniversityEmployeeDo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UniversityEmployeeInformation.CommonObjects
{
public class UniversityEmployeeDo
{
public int EmployeeId { get; set; }

public string EmployeeName{get;set;}
public string EmployeeDepartment { get; set; }


}
}

UniversityEmployeeDTO

using System;
using System.Runtime.Serialization;
using System.Collections.Generic;

namespace UniversityEmployeeInformation.CommonObjects
{


public class UniversityEmployeeDTO
{
private List _employees;

public List Employees
{
get { return _employees; }
set { _employees = value; }
}
}
}

UniversityEmployeeRepository

Interface

UniversityEmployeeRep
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;

namespace UniversityEmployeeInformation.UniversityEmployeeRepository.Interface
{
public interface IUniversityEmployeeRep
{
List GetEmployees(EmployeeFilter employeeFilter);
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo);
}
}

UniversityEmployeeRep

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using System.Data.SqlClient;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;

namespace UniversityEmployeeInformation.UniversityEmployeeRepository
{
public class UniversityEmployeeRep : IUniversityEmployeeRep
{

public List GetEmployees(EmployeeFilter employeeFilter)
{
List employees = new List();
SqlConnection con =
new SqlConnection("Data Source=XYZ;Initial Catalog=EmployeeDB;Integrated Security=True");
con.Open();
string commandText = "select * from dbo.Employee where EmployeeId=" + employeeFilter.EmployeeId;
SqlCommand cmd = new SqlCommand(commandText, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
UniversityEmployeeDo employeeDo = new UniversityEmployeeDo();
employeeDo.EmployeeId = Convert.ToInt32(dr["EmployeeId"]);
employeeDo.EmployeeDepartment = dr["EmployeeName"].ToString();
employeeDo.EmployeeName = dr["EmployeeDepartment"].ToString();
employees.Add(employeeDo);
}
return employees;
}

public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo)
{
throw new NotImplementedException();
}
}
}


UniversityManager

Interfacce

IUniversityEmployeeManager
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;

namespace UniversityEmployeeInformation.Manager.Interface
{
public interface IUniversityEmployeeManager
{
List GEmployees(EmployeeFilter employeeFilter);
}
}

UniversityEmployeeManager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using UniversityEmployeeInformation.UniversityEmployeeRepository;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;
using UniversityEmployeeInformation.Manager.Interface;


namespace EmployeeInformation.Manager
{
public class UniversityEmployeeManager:IUniversityEmployeeManager
{
private IUniversityEmployeeRep _employee;

public IUniversityEmployeeRep Employee
{
set { _employee = value; }
}
public List GEmployees(EmployeeFilter employeeFilter)
{
_employee = new UniversityEmployeeRep();
return _employee.GetEmployees(employeeFilter);
}
}
}

Let me create the service

UniversityEmployeeService

Interface
IUniversityEmployeeService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using UniversityEmployeeInformation.UniversityEmployeeRepository;


namespace EmployeeInformation.EmployeeService.Interface
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IUniversityEmployeeService
{
[OperationContract]
[FaultContract(typeof(ServiceFault))]
List GetEmployees(EmployeeFilter employeeFilter);


[OperationContract]
[FaultContract(typeof(ServiceFault))]
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter);
}


// Use a data contract as illustrated in the sample below to add composite types to service operations

}

UniversityEmployeeService


using System;
using System.Collections.Generic;
using System.ServiceModel;
using UniversityEmployeeInformation.CommonObjects;
using EmployeeInformation.EmployeeService.Interface;
using UniversityEmployeeInformation.UniversityEmployeeRepository;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;
using UniversityEmployeeInformation.Manager.Interface;


namespace UniversityEmployeeInformation.UniversityEmployeeService.Service
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class UniversityEmployeeService : IUniversityEmployeeService
{
private IUniversityEmployeeRep _employeeRepository;
private ServiceFault _fault;

public UniversityEmployeeService()
{
_employeeRepository = new UniversityEmployeeRep();

}
public UniversityEmployeeService(IUniversityEmployeeRep employee)
{
_employeeRepository = employee;
}
private IUniversityEmployeeManager _employeeManager;
public List GetEmployees(EmployeeFilter employeeFilter)
{

return _employeeRepository.GetEmployees(employeeFilter);
}

public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter)
{
try
{



if (employeeFilter.EmployeeId <= 0) { throw new MessageException("Invalid EmployeeId"); } UniversityEmployeeDo employee = _employeeRepository.GetEmployees(employeeFilter)[0]; UniversityEmployeeDo employeeDo = _employeeRepository.UpdateEmployee(employeeFilter, employee); return employeeDo; } catch (Exception ex) { _fault = new ServiceFault(){FaultException = (MessageException) ex}; throw new FaultException(_fault, new FaultReason(string.Empty));
}

}


}
}


Let us create Service Test Project
UniversityEmployeeServiceTest

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel;
using System.Text;
using UniversityEmployeeInformation.CommonObjects;
using System.Data.SqlClient;
using UniversityEmployeeInformation.UniversityEmployeeService.Service;

using NUnit;
using NUnit.Framework;
using Rhino.Mocks;
using UniversityEmployeeInformation.UniversityEmployeeRepository;
using UniversityEmployeeInformation.UniversityEmployeeRepository.Interface;

namespace UniversityEmployeeServiceTest
{

[TestFixture, System.ComponentModel.Description("EmployeeRepository Test")]
public class UniversityEmployeeServiceTest
{
private MockRepository _mockRepository;
private UniversityEmployeeService _employeeService;
private ServiceFault _fault;

[TestFixtureTearDown]
public void TestFixtureTearDown()
{

}

[SetUp]
public void Initialize()
{

_employeeService = new UniversityEmployeeService { };
_mockRepository = new MockRepository();
}

[Test]
public void GetEmployeeServiceIntegrationTest()
{
EmployeeFilter employeeFilter = new EmployeeFilter();
employeeFilter.EmployeeId = 1223;
_employeeService = new UniversityEmployeeService();
List employees = _employeeService.GetEmployees(employeeFilter);
Assert.IsNotNull(employees);
}
[Test]
public void GetEmployee_ReturnsExpected_Employee()
{
var employees = new List();
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo();
employeeDo1.EmployeeId = 1223;
employeeDo1.EmployeeName = "ABC";
employeeDo1.EmployeeDepartment = "XYZ";
employeeDo2.EmployeeId = 123443;
employeeDo2.EmployeeName = "XYZ";
employeeDo2.EmployeeDepartment = "HIK";
employees.Add(employeeDo1);
employees.Add(employeeDo2);

var mockEmployeeRepository = _mockRepository.StrictMock();
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employees);

}
_mockRepository.ReplayAll();
List employeesResult = _employeeService.GetEmployees(employeeFilter);
_mockRepository.VerifyAll();
Assert.AreSame(employees, employeesResult);

}

[Test]
public void GetEmployee_ReturnsNotExpected_Employee_ServiceMockTest()
{
var actualemployees = new List();
var returnemployees = new List();
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo();
employeeDo1.EmployeeId = 1223;
employeeDo1.EmployeeName = "ABC";
employeeDo1.EmployeeDepartment = "XYZ";
employeeDo2.EmployeeId = 123443;
employeeDo2.EmployeeName = "XYZ";
employeeDo2.EmployeeDepartment = "HIK";
returnemployees.Add(employeeDo1);
actualemployees.Add(employeeDo2);
actualemployees.Add(employeeDo2);
var mockEmployeeRepository = _mockRepository.StrictMock();
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = 123443 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(returnemployees);

}
_mockRepository.ReplayAll();
List employeesResult = _employeeService.GetEmployees(employeeFilter);
_mockRepository.VerifyAll();
Assert.AreNotEqual(actualemployees, employeesResult);

}

[Test]
public void UpdateEmployee_Will_Return_EmployeeDo_with_Updates()
{

var inputEmployeeDo = new UniversityEmployeeDo();
List employeeList = new List();
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock();
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = 123 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);

}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();

Assert.AreSame(updatedEmployeeDo.EmployeeDepartment, employee.EmployeeDepartment);
}


[Test]
[ExpectedException(typeof(MessageException), ExpectedMessage = "Invalid EmployeeId")]
public void Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero()
{
try
{
var inputEmployeeDo = new UniversityEmployeeDo();
List employeeList = new List();
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock();
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = -1235 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);

}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();


}
catch (Exception ex)
{
throw ((System.ServiceModel.FaultException) (ex)).Detail.
FaultException;
}
}
}
}

This is a different kind of architecture where there will be Repository,Manager ,Comon Objects,DTOs and Service,
I used all these and created advance Mock Test cases for Service ,I used an example called UniversityEmployee Information
First part explains about the Domain Common Objects UniversityEmployeeDo which has the three properties

public int EmployeeId { get; set; }
public string EmployeeName{get;set;}
public string EmployeeDepartment { get; set; }
And also a EmployeeFilter Do with two Properties
public int EmployeeId { get; set; }
public string EmployeeDept { get; set; }
i also added MessageException and a DTO
UniversityEmployeeRepository
This is the Repository where we access the database or Update the database and so
I created an interface IUniversityEmployeeRep where i declared two methods
List GetEmployees(EmployeeFilter employeeFilter);
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo);

If you Observed the two Methods in implementation
The first one
public List GetEmployees(EmployeeFilter employeeFilter)
{
List employees = new List();
SqlConnection con =
new SqlConnection("Data Source=ABRAR-PC;Initial Catalog=EmployeeDB;Integrated Security=True");
con.Open();
string commandText = "select * from dbo.Employee where EmployeeId=" + employeeFilter.EmployeeId;
SqlCommand cmd = new SqlCommand(commandText, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
UniversityEmployeeDo employeeDo = new UniversityEmployeeDo();
employeeDo.EmployeeId = Convert.ToInt32(dr["EmployeeId"]);
employeeDo.EmployeeDepartment = dr["EmployeeName"].ToString();
employeeDo.EmployeeName = dr["EmployeeDepartment"].ToString();
employees.Add(employeeDo);
}
return employees;
}

Here, I am returning the employee list fetching from the database,there is another method
public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter, UniversityEmployeeDo employeeDo)
{
throw new NotImplementedException();
}
UpdateEmployee which is not yet implemented and it has NotImplementedException throw Exception

I am using these two repository methods directly in Service
I created Service called UniversityEmployeeService which is a WCF Service and has two methods
Interface:
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IUniversityEmployeeService
{
[OperationContract]
[FaultContract(typeof(ServiceFault))]
List GetEmployees(EmployeeFilter employeeFilter);


[OperationContract]
[FaultContract(typeof(ServiceFault))]
UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter);
}
And In Service
private IUniversityEmployeeRep _employeeRepository;
private ServiceFault _fault;

public UniversityEmployeeService()
{
_employeeRepository = new UniversityEmployeeRep();

}
public UniversityEmployeeService(IUniversityEmployeeRep employee)
{
_employeeRepository = employee;
}
private IUniversityEmployeeManager _employeeManager;
public List GetEmployees(EmployeeFilter employeeFilter)
{

return _employeeRepository.GetEmployees(employeeFilter);
}

public UniversityEmployeeDo UpdateEmployee(EmployeeFilter employeeFilter)
{
try
{



if (employeeFilter.EmployeeId <= 0) { throw new MessageException("Invalid EmployeeId"); } UniversityEmployeeDo employee = _employeeRepository.GetEmployees(employeeFilter)[0]; UniversityEmployeeDo employeeDo = _employeeRepository.UpdateEmployee(employeeFilter, employee); return employeeDo; } catch (Exception ex) { _fault = new ServiceFault(){FaultException = (MessageException) ex}; throw new FaultException(_fault, new FaultReason(string.Empty));
}

}
In Service I created Service constructor where I am going to inject the Mock Repository

In this Service I have two methods
1. GetEmployees
2. UpdateEmployee
Since i added Repository code in GetEmployee but i have not yet added and any logic in Updated Employee Repository method,but i create the update service method,i know what validations i need to put and check for the employee in Mock service Test case


Let us now work around the mock test cases
[Test]
public void GetEmployee_ReturnsExpected_Employee()
{
var employees = new List();
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo();
employeeDo1.EmployeeId = 1223;
employeeDo1.EmployeeName = "ABC";
employeeDo1.EmployeeDepartment = "XYZ";
employeeDo2.EmployeeId = 123443;
employeeDo2.EmployeeName = "XYZ";
employeeDo2.EmployeeDepartment = "HIK";
employees.Add(employeeDo1);
employees.Add(employeeDo2);

var mockEmployeeRepository = _mockRepository.StrictMock();
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employees);

}
_mockRepository.ReplayAll();
List employeesResult = _employeeService.GetEmployees(employeeFilter);
_mockRepository.VerifyAll();
Assert.AreSame(employees, employeesResult);

}

In this test case GetEmployee_ReturnsExpected_Employee will returns me the expected employee
I declare two Employees
var employeeDo1 = new UniversityEmployeeDo();
var employeeDo2 = new UniversityEmployeeDo(); and added to a list
and in Mock test i am asking return me only these two employees
and it will return me only these two employees without fetching it from the database because i mock the employee Repository Using
var mockEmployeeRepository = _mockRepository.StrictMock();

In this my test case will be always pass ,it will be failed if someone changes the logic in Service
There is another benefit of using mock is that I explain in the next test case
[Test]
public void UpdateEmployee_Will_Return_EmployeeDo_with_Updates()
{

var inputEmployeeDo = new UniversityEmployeeDo();
List employeeList = new List();
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock();
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = 123 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);

}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();

Assert.AreSame(updatedEmployeeDo.EmployeeDepartment, employee.EmployeeDepartment);
}

In this test case _ UpdateEmployee_Will_Return_EmployeeDo_with_Updates()
I am askign to update the employee and return me updated UniversityEmployeeDo

For Exception Test case we can write like this I created one test case
[Test]
[ExpectedException(typeof(MessageException), ExpectedMessage = "Invalid EmployeeId")]
public void Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero()
{

var inputEmployeeDo = new UniversityEmployeeDo();
List employeeList = new List();
var updatedEmployeeDo = new UniversityEmployeeDo();
inputEmployeeDo.EmployeeId = 1223;
inputEmployeeDo.EmployeeName = "ABC";
inputEmployeeDo.EmployeeDepartment = "BPO";
updatedEmployeeDo.EmployeeId = 1223;
updatedEmployeeDo.EmployeeName = "ABC";
updatedEmployeeDo.EmployeeDepartment = "DEVELOPMENT";
employeeList.Add(inputEmployeeDo);
var mockEmployeeRepository = _mockRepository.StrictMock();
_employeeService = new UniversityEmployeeService(mockEmployeeRepository);
var employeeFilter = new EmployeeFilter() { EmployeeId = -1235 };
using (_mockRepository.Ordered())
{
Expect.Call(mockEmployeeRepository.GetEmployees(employeeFilter)).Return(employeeList);
Expect.Call(mockEmployeeRepository.UpdateEmployee(employeeFilter, inputEmployeeDo)).Return(updatedEmployeeDo);

}
_mockRepository.ReplayAll();
UniversityEmployeeDo employee
= _employeeService.UpdateEmployee(employeeFilter);
_mockRepository.VerifyAll();



}

This Test case say throw MessageException if i passed a Invalid employee Id
Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero





Working Solution will be provide based on your demand
on comments

Sunday, August 7, 2011

Sharepoint Object Model

Introduction to Sharepoint Object Model


Sharepoint Object Model can be implemented using both at client and at server side where in sharepoint 2010 introduce the Client Object Model,
Sharepoint object model is heart of sharepoint programming and sharepoint stuff,it is mainly deals with Sharepoint Objects which i am going to disscuss more in next few minutes

Microsoft Company has spent a very huge amount of time developing .NET namespaces for SharePoint which includes both the versions Moss 2007 and Sharepoint 2010.
Since Microsoft has provide a set of namespaces which allows programmer to have an access to a Windows Sharepoint Services or Sharepoint Foundation and Sharepoint 2010 or Sharepoint 2007
The SharePoint Services object model is extensive, to say the least.
There are lots of namespaces within the object model and dozens of classes covering most of the
features of SharePoint 2010 and 2007. To understand the architectural design makes it impractical to study the object model directly. Instead of that it is better to use the object model to solve categories of solutions that we are creating.

using the objects in the SharePoint model is done in a similar manner as we do to any hierarchical object model
The key to navigating such a model
is to find the starting point for the model. In SharePoint, this is done through the ASP.NET
Context object using the following code:
SPSite site = SPControl.GetContextSite(Context);
there is one more class SPControl which inherities from the namespace Microsoft.SharePoint.WebControls,we are not required to create an instace of the Spcontrol object,simply use GetContextSite method provided and use and pass the Context property
which comes from the namespace System.Web.UI.Page,which will available to Webpart and Pages

site collection which holds all lists and other stuff.
SPSite objects contain information about the site collection and the sites within it. In
order to access any particular site in the collection, you must return a collection of SPWeb
objects. You may then access the individual web sites by enumerating them or accessing
one directly through an index like
SPWeb site = SPControl.GetContextWeb(Context);

Accessing Lists and List Items

Usually site collections,holes information about access lists and list items ,where we use mainly to accss list information frequently.When we are working with mainly
with lists,we are interested in a particular site rather than a site collection. Usually we do a
a reference to an individual site by using the GetContextWeb method of the SPControl object.
Once a site is open,We may access all of the lists it contains through the SPListCollection
object. Each site collection holds an SPList object for every list on the web site. Let us consider the example of partial code sample

SPWeb site = SPControl.GetContextWeb(Context);
SPListCollection lists= site.Lists;
foreach(SPList list in lists)
{
//add code here
}
I just list out the few lists here where we use object model more frequently
Annocement List
DiscussionBoard
DocumentLibrary
GenericList
Issue
Survey

Will be discuss more in future posts

Assert and Descriptions,in Implementing Rhino Mocks Using Visual Studio

Ordered and Unordered

This helps in how the methods are being called,you can specify ordered or Unordered
Ordered--> it will check for same order which you specify in you mock test case with your actual method calling methods and objects
UnOrdered:->you no need to specify the order of the methods in mock test and it will will execute by default order

Expect.Call

Expect call will check for specified method is being called for example the test case public void GetStudentMockTest() in my earlier post i declared
Expect.Call(mockStudentRepository.GetStudentDetails()).Return(student1).IgnoreArguments();
here i am asking my test case to see in my service method it calls the GetStudentDetails() method,
It will return me student1 object


How to use for Call Repetition

Repeat.Any
Repeat.Any is mainly every important when you have test case where you are using foreach
for example let us say you have method call insertnew student record
and you have service where you are inserting multiple records at one time

public string submitStudnetRecors(List students)

{
foreach(studnet in students)
{
StudentBal student=new StudentBal();
studentbal.InsertNewstudent((studnet)


}
}

In this your repository would be like this

Expect.Call(mockStudentRepository.InsertNewstudent((studnet)).Repeat.Any.Return(message)

and other
Repeat.Once() ==> the default
Repeat.Any() ==> for number of times
Repeat.Never()
Repeat.AtLeastOnce()
Repeat.Twice()
Repeat.Times(13)
Repeat.Times(2, int.MaxValue)

Mock test with Void Methods

Sometimes we may also face situation where we want to do a mock test for void methods
example:
public void sendEmail(emailDO)
{
your code here;
}
in this case you need to declare like this
Expect.Call(() => mailRepository.sendEmail(emailDO))

Ignore Argument Constraints

Expect.Call(mockRepository.GetEmployeeDetails(EmployeeId)).Return(EmployeeObject) IgnoreArguments()

Here IgnoreArguments() is the keyword used here it will just ignore any arguments we passed ,and it just return whatever we mentioned in the Return, example here it just returns the EmployeeObject Return(EmployeeObject)

Some More Useful Assert statements

Is.Anything()
Is.Equal(3) ,Is.NotEqual(42) (this is applicable even for objects but need to write equal override for the object
)
Is.Same(obj) for objects
Is.NotSame(obj) ) for objects
Is.Null() it Checks for NULL values
Is.NotNull()
Is.GreaterThan(3)
Is.GreaterThanOrEqual(3)
Is.LessThan(3)
Is.LessThanOrEqual(3)

Text.StartsWith("hi Rhino Mock");
Text.EndsWith("Good Bye");
Text.Contains("Sachin Tendulkar");


For Exception Type Testing for Rhino Mock Test cases

[Test]
[ExpectedException(typeof(BusinessException), ExpectedMessage = "Invalid EmployeeId")]
public void Update_Employee_will_Return_Excception_If_The_EmployeeId_Is_Less_than_OR_equalToZero()
{
//Test Case code here where exception occurs and will check for exception type and message if any
}

Saturday, August 6, 2011

Creating SharePoint 2010 Visual Web Parts in Visual Studio 2010

Create SharePoint 2010 Visual Web Parts in Visual Studio 2010

This is my first post on sharepoint 2010,I am creating a sharepoint 2010 visual webpart using visual studio 2010


1.Select Visual Studio 2010





figure 1





As shown in the figure 1



Give some name say HelloWorldVisualWebpart and click on for next screen,the screen will be similar like this as shown in figure 2



Figure 2







It is noticed that Deployed as Sandboxed Solution is Gray Out since visual webpart cannot be deployed as sandboxed solution.

Click Next solution Exloper will be displayed similar to shown in figure 3


.



Figure 3






You could Noticed that it creates files

1. Elements.xml

2. VisualWebPart1.cs


3. VisualWebPart1.webpart

4. VisualWebPart1UserControl.asx controls

It is also noticed that

The ascx control will be deployed in the following following folder

_CONTROLTEMPLATES/HelloWorldVisualWebpart/VisualWebPart1/VisualWebPart1UserControl.ascx


Let us write the code in VisualWebpart .cs file

using System;


using System.ComponentModel;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebC

ontrols;

namespace HelloWorldVisualWebpart.VisualWebPart1

{

[ToolboxItemAttribute(false)]


public class VisualWebPart1 : WebPart

{

// Visual Studio might automatically update this path when you change the Visual Web Part project item.

private const string _ascxPath = @"~/_CONTROLTEMPLATES/HelloWorldVisualWebpart/VisualWebPart1/VisualWebPart1UserControl.ascx";

protected override void CreateChildControls()

{


Control control = Page.LoadControl(_ascxPath);

Label lbl = new Label();


lbl.Text = "MY FIRST,WEBPART FROM HELLO WORLD FROM VISUAL WEBPART";

Controls.Add(lbl);

Controls.Add(control);


}

}

}


I added the code in create Child Controls method which is similar to page load method in normal Asp.net Page,

I declared the Control variable and load the Ascx control,and also i created lable

Label lbl = new Label();

This statement will create a lable and i added the text to lable text and finally i added lable to contol.


Now Build the Soultion using Option Build which internally creates WSP solution and also it will deploy as Farm Solution , coding for Visual Webpart is completed


Deploy webpart and Add a Webpart in Home Page

In Galleries Section Click on Webparts which will displayed list of Webparts already deployed in the Portal.







Now we are deploying our webpart in the Home Page i used Home page in this example you can deploy the webpart on any page as you like,depends on the requirements,by default the webpart will be in Custom Category,if you want to make the changes like Title and description of webpart can also be possible by editing the webpart properties.

Now i deploye the webpart in Home Page it looks similar to like this













Thanks,
Next will be more advance topics in sharepoint i am going to post soon.....

Wednesday, August 3, 2011

Best Practices using Visual Studio 2010 --http://www.microsoft.com/india/msdn/visualstudio2010/bestpractices.aspx

Best Practices using Visual Studio 2010 --http://www.microsoft.com/india/msdn/visualstudio2010/bestpractices.aspx


Good one can Registration for all lead ,develBulleted Listoper or testers can register , to know more best practices using Visual Studio 2010 for all locations ,Bangalore,Hyderabad,Mumbai