Split off from http://forums.asp.net/t/1104119.aspx/1?Test+Driven+Development+and+3+tier+architecture.
latticesoft
Morrigu
I would appreciate it if someone can link me to an article or tutorial that discusses solutions for this kind of problem. Or otherwise give some hints regarding the issue.
The basic idea behind the TDD is that to test your application as earlier as possible. As we all know the biggest challange facing the software developement is change request from the business, and the TDD's answer is test, test and test!
You should test your 3-tier seperately, below is the steps we follow for testing 3-tier application:
1. Write NUnit test scripts for DAL
2. Write NUnit test scripts for BLL (sure you can use Mock object here if you do not hava a DAL yet).
You do not need Mock objects if you only need some data returned from DAL, just make some dummy data at DAL and return to BLL, something like:
The examples in the books and blog makes sense ,when testing the occurence of a charater in the string and testing the Stack's Pop function.Considering N Tier Application,where we have UI,Business Tier and Data Tier.The Data Tier in turns calls the required
Stored Procedures and gets the data.
The concept behind TDD is to perform tests in isolation,which means that we have to Mock or Fake the Data.
But my doubts about this methodology is that what should the TDD test.
What I have understood is that ,it tests whether the GetCustomer function returns the expected result.
Now my question is what if the Stored Procedure has a Bug,the TDD wont catch that Bug as the Data is not extracted using the Buggy Stored Procedure.
Also how to test a Business & Data Tier function which calls a Stored Procedure and the Stored Procedure has all the Business Rules implemented.
For testing CRUD is out of TDD, what you want to do are integration tests.
But before you can do this, you must be aware, that you can't do any logic in your storedprocedure.
A stored procedure is located in your database, and as it's telling database = data. BusinessLogic = Logical programming.
So you can add a test project. You have your data entities which are mapping on your database.
Create a test and use reflection, to get the colums and datatypes. Based on the datatype, you can set a value for the column.
Create a record and get the id
Update the record with the id
Read the record with the id
Remove the record with the id
And create more then 1row while your executing this!
Some sample code:
For Each m As MemberInfo In data.GetType.GetMembers()
If m.MemberType = MemberTypes.Property Then
prop = data.GetType.GetProperty(m.Name)
If Not prop.Name.Equals("Id") AndAlso prop.Name.EndsWith("Id") Then
If Not childmock Is Nothing Then
prop.SetValue(data, childmock.Id, New Object() {})
Else
prop.SetValue(data, Nothing, New Object() {})
End If
ElseIf prop.PropertyType Is GetType(String) Then
prop.SetValue(data, "x", New Object() {})
ElseIf (prop.PropertyType Is GetType(Integer) OrElse prop.PropertyType Is GetType(Nullable(Of Integer))) AndAlso Not prop.Name.Equals("Id") Then
prop.SetValue(data, 1, New Object() {})
ElseIf prop.PropertyType Is GetType(Decimal) OrElse prop.PropertyType Is GetType(Nullable(Of Decimal)) Then
prop.SetValue(data, 1.23D, New Object() {})
ElseIf prop.PropertyType Is GetType(Boolean) OrElse prop.PropertyType Is GetType(Nullable(Of Boolean)) Then
prop.SetValue(data, False, New Object() {})
ElseIf prop.PropertyType Is GetType(DateTime) OrElse prop.PropertyType Is GetType(Nullable(Of DateTime)) Then
prop.SetValue(data, Date.Now, New Object() {})
ElseIf prop.PropertyType Is GetType(Date) OrElse prop.PropertyType Is GetType(Nullable(Of Date)) Then
prop.SetValue(data, Date.Now, New Object() {})
ElseIf prop.PropertyType Is GetType(Byte()) Then
Dim b As New List(Of Byte)
b.Add(New Byte())
prop.SetValue(data, b.ToArray, New Object() {})
Else
prop.SetValue(data, Nothing, New Object() {})
End If
End If
Next
Please click 'Mark as Answer' if my reply has assisted you
Check My Blog
jigsmshah
Member
2 Points
8 Posts
Test Driven Development and 3-tier architecture
Jan 04, 2012 03:10 PM|LINK
Split off from http://forums.asp.net/t/1104119.aspx/1?Test+Driven+Development+and+3+tier+architecture.
I am learning TDD and Unit Testing.
The examples in the books and blog makes sense ,when testing the occurence of a charater in the string and testing the Stack's Pop function.Considering N Tier Application,where we have UI,Business Tier and Data Tier.The Data Tier in turns calls the required Stored Procedures and gets the data.
The concept behind TDD is to perform tests in isolation,which means that we have to Mock or Fake the Data.
But my doubts about this methodology is that what should the TDD test.
What I have understood is that ,it tests whether the GetCustomer function returns the expected result.
Now my question is what if the Stored Procedure has a Bug,the TDD wont catch that Bug as the Data is not extracted using the Buggy Stored Procedure.
Also how to test a Business & Data Tier function which calls a Stored Procedure and the Stored Procedure has all the Business Rules implemented.
Also how to implement TDD for CRUD operations?
Regards
Boere
Member
560 Points
114 Posts
Re: Test Driven Development and 3-tier architecture
Jan 06, 2012 10:31 AM|LINK
For testing CRUD is out of TDD, what you want to do are integration tests.
But before you can do this, you must be aware, that you can't do any logic in your storedprocedure.
A stored procedure is located in your database, and as it's telling database = data. BusinessLogic = Logical programming.
So you can add a test project. You have your data entities which are mapping on your database.
Create a test and use reflection, to get the colums and datatypes. Based on the datatype, you can set a value for the column.
Create a record and get the id
Update the record with the id
Read the record with the id
Remove the record with the id
And create more then 1row while your executing this!
Some sample code:
For Each m As MemberInfo In data.GetType.GetMembers()
If m.MemberType = MemberTypes.Property Then
prop = data.GetType.GetProperty(m.Name)
If Not prop.Name.Equals("Id") AndAlso prop.Name.EndsWith("Id") Then
childmock = GetMock(prop.Name.Remove(prop.Name.Length - 2, 2))
If Not childmock Is Nothing Then
prop.SetValue(data, childmock.Id, New Object() {})
Else
prop.SetValue(data, Nothing, New Object() {})
End If
ElseIf prop.PropertyType Is GetType(String) Then
prop.SetValue(data, "x", New Object() {})
ElseIf (prop.PropertyType Is GetType(Integer) OrElse prop.PropertyType Is GetType(Nullable(Of Integer))) AndAlso Not prop.Name.Equals("Id") Then
prop.SetValue(data, 1, New Object() {})
ElseIf prop.PropertyType Is GetType(Decimal) OrElse prop.PropertyType Is GetType(Nullable(Of Decimal)) Then
prop.SetValue(data, 1.23D, New Object() {})
ElseIf prop.PropertyType Is GetType(Boolean) OrElse prop.PropertyType Is GetType(Nullable(Of Boolean)) Then
prop.SetValue(data, False, New Object() {})
ElseIf prop.PropertyType Is GetType(DateTime) OrElse prop.PropertyType Is GetType(Nullable(Of DateTime)) Then
prop.SetValue(data, Date.Now, New Object() {})
ElseIf prop.PropertyType Is GetType(Date) OrElse prop.PropertyType Is GetType(Nullable(Of Date)) Then
prop.SetValue(data, Date.Now, New Object() {})
ElseIf prop.PropertyType Is GetType(Byte()) Then
Dim b As New List(Of Byte)
b.Add(New Byte())
prop.SetValue(data, b.ToArray, New Object() {})
Else
prop.SetValue(data, Nothing, New Object() {})
End If
End If
Next
Check My Blog
atconway
All-Star
16846 Points
2756 Posts
Re: Test Driven Development and 3-tier architecture
Jan 09, 2012 01:31 PM|LINK
You could apply TDD to the database objects with the following methodology:
Apply Test-Driven Development to your Database Projects:
http://msdn.microsoft.com/en-us/magazine/cc164243.aspx