This is a migrated thread and some comments may be shown as answers.

Best practices of unit testing with OpenAccess

18 Answers 289 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alexey
Top achievements
Rank 2
Alexey asked on 10 Nov 2008, 01:15 PM
Have you got any practices or recommendation of unit testing while using open access? I failed to find any examples how it can be done in documetation.

18 Answers, 1 is accepted

Sort by
0
Jan Blessenohl
Telerik team
answered on 10 Nov 2008, 02:48 PM
Hello Alexey,

There are no mock objects for unit testing at the moment. We just try to clean the database content in the testcase setup method and additional after the tests in the cleanup.

All the best,
Jan Blessenohl
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Erik
Top achievements
Rank 1
answered on 16 Mar 2009, 03:41 PM
Hi Alexey,

Are you talking about unit testing (the functionality contained in) your domain objects? 

From the top of my head, an option could be to derive an object from IObjectScopeProvider that implements your database happy path (i.e. returns the objects you expect to get from the database when everything works on the OpenAccess end) and then use a mocking framework or some dependency injection to call the functions in your IObjectScopeProvider object whenever a call is made to the original objectScopeProvider object as created by OpenAccess.

Does this make any sense? Since I am implementing unit testing for one of my projects anyway I'll see if I can get something up to provide an example.

Regards,
Erik
0
Dimitar Kapitanov
Telerik team
answered on 16 Mar 2009, 05:57 PM
Hello Erik,
Please check this topic from our documentation and tell us what you think of the content there:
http://www.telerik.com/help/openaccess-orm/openaccess-tasks-howto-using-openaccess-with-test-frameworks.html.
Hope this helps.

All the best,
Dimitar Kapitanov
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Erik
Top achievements
Rank 1
answered on 17 Mar 2009, 01:08 PM
Hi Dimitar,

The tutorial provided by the link is excellent and I am using that same approach when I am testing my application/objects interface with the database. However, it does not provide a way to test your business objects and business logic independent of OpenAccess.

For example, I have a persistent Person class. I can use your approach to test whether I can add this class to the database successfully. That's great and very useful indeed.

However, I also have a PersonService class that encapsulates some business logic involved in adding people to the database. For example, I might want to keep track of a blacklist of people that I do not want to have added to the database. As the PersonService uses it's own instance of the ObjectScope, the automatic rollback provided by your example doesn't work.

In addition to that, I would like to be able to test just the business logic, not the interface to the database. That is why I suggested mocking to abstract out the database access.

I am currently working through the example I outlined above to see if mocking would be possible. I will certainly keep you posted on my progress.

Regards,
Erik
0
Dimitar Kapitanov
Telerik team
answered on 17 Mar 2009, 01:29 PM
Hi Erik,
Well there are supporters of both approaches. Let me explain why I prefer using test databases against database mocks: by using a test database, you open up the possibility that problems could be caused at the database itself or along the communication path (network, etc) between the DAL and database. Mocking eliminates those possibilities. And in the end mocks hinder the unit tests because you are not taking into account a series of real world problems that usually have impact on your behavior. I think that it would be much more productive to have those things sorted out in the testing phase. From my perspective if you test only BOs using mocks of the DAL and everything is OK, and then in the real case situation you hit a showstopper because of the DAL, this puts you in the same awful situation. Of course that is my opinion, and I do not believe that we can put a statement what best practices are, or are not. Everyone should decide based on the context of the task is my opinion.

All the best,
Dimitar Kapitanov
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Erik
Top achievements
Rank 1
answered on 17 Mar 2009, 02:01 PM
Hi Dimitar,

You are absolutely right, everyone should make the best choice for their own particular situation. For myself, I always (try to) test both the actual database access (as you stated, a lot can go wrong there) and use mocking only when I am testing just the business logic. For me personally, that gives me the peace of mind that I am approaching testing from all possible angles.

Great discussing this with you by the way, it's a sure sign you take these things very seriously :-)

Regards,
Erik
0
Erik
Top achievements
Rank 1
answered on 17 Mar 2009, 03:54 PM
Hi Guys,

As promised, I did some tinkering about with OpenAccess and mocking. I've written a short article on it on my blog.


I hope you'll find it useful.

Regards,
Erik
0
Dimitar Kapitanov
Telerik team
answered on 18 Mar 2009, 08:13 AM
Hello Erik,
It was a really nice post. I enjoyed reading it.

Greetings,
Dimitar Kapitanov
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Nikesh
Top achievements
Rank 1
answered on 06 Sep 2012, 04:46 PM
Hi 

I've tried using the testing frame work in class "PersonserviceTestsWithoutMocking" as discussed in the following article http://www.reversealchemy.net/2009/03/17/exploring-telerik-openaccess-unit-testing-your-business-logic-using-rhino-mocks/, However the code looks it very old as it uses ObjectScopeProvider1 and I can't find that object in the latest Free Edition of Open Access ORM.


I then read this article and applied the code changes to my model: http://www.telerik.com/community/forums/orm/development/where-is-objectscopeprovider1.aspx, This seemed to work however how do I access the entities within my model via the scope variable?In PersonserviceTestsWithoutMocking example they wrote the following:

Person sparrow = scope.Extent().SingleOrDefault( p => p.LastName.Equals( "Sparrow" ) );

However when I write the same piece of code it can't find the type Person (or in my case object Security which is in my model).

Any suggestions would be great



0
James Legan
Top achievements
Rank 1
answered on 06 Sep 2012, 08:15 PM
Tagged for feedback. I have been searching all day for a usable example for unit testing with NUnit, JustMock, etc... that has a relevant example using current OpenAccess methods/implementations. Even the documentation seems to be out of date.
0
Nikesh
Top achievements
Rank 1
answered on 10 Sep 2012, 11:46 AM
Is there anyone from Telerik that can help with this issue?
0
Ivailo
Telerik team
answered on 11 Sep 2012, 11:46 AM
Hi,

We are currently offering unit testing examples for many of our OpenAccess SDK sample projects. You can download the SDK here and use the Open Test Solution button in order to access the sample together with its unit tests. 

Let us know if those samples do not fully cover the topic or you have more specific questions on unit testing.


Regards,
Ivailo
the Telerik team
Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
0
Nikesh
Top achievements
Rank 1
answered on 12 Sep 2012, 06:29 PM
Hi

Thanks for getting back to me. I've looked at the SDK and opened up the test solutions but all the tests I've looked at test the service or Mock the DAL level. What I want to do is test the actual ORM. So if I have an entity called Car I want to create a test to add, test to update and test to delete it and verify that the ORM can perform these operations. When I run the test it runs it in a transaction statement so that once the tests passes or fails it roll-backs the changes. Something like "PersonserviceTestsWithoutMocking" as discussed in the following article http://www.reversealchemy.net/2009/03/17/exploring-telerik-openaccess-unit-testing-your-business-logic-using-rhino-mocks/

Can someone point me in the right direction?

Regards
0
PetarP
Telerik team
answered on 17 Sep 2012, 03:13 PM
Hi Nikesh,

 you can always "commit" your changes with a context.Flush(). This way you will be able to see the actual changes in the database thus knowing that everything has worked out as expected and at the end in the TestCleanup you will still be able to call context.RollBack so that all changes are rolled back.
Will that work for you?

All the best,
Petar
the Telerik team
Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
0
James Legan
Top achievements
Rank 1
answered on 18 Sep 2012, 12:59 PM
Nikesh,







Please take a look at the documentation below and if it is what you are looking for, I sent the entire program to Telerik yesterday on a ticket and I can find a way to get it to you. I went on a little journey last week to learn mocking and JustMock better as well as figure out a way to test "real" databases in memory and came up with two distinct ways to test.

 

dbMockTest Write Up:

The goal of this sample program is show two very different yet useful ways of testing CRUD operations that rely on business logic. This type of scenario is very common in workflows where inserts, updates and deletes have constraints which are enforced by both requirements as well as foreign key constraints in SQL server.

This demo app includes the following:

1)      MDF file for sample database used in the application.

2)      Visual Studio 2012 solution with two projects (program and unit test).

Requirements:

1)      OpenAccess ORM (free to download via NuGet or available at Telerik.com)



 
2)      NUnit (free to download via NuGet)



 
3)      SQL Server 2012 LocalDB (free from Microsoft.com) or some other SQL Server. If another SQL server product is used, you will have to edit the connection strings. MDF file is include that can be attached to LocalDB through the management tools.



 
4)      JustMock Free (available from Telerik.com)


What it does:

In the main project, Main in Program.cs carries out a very basic set of operations against a “live” SQL database. For the purposes of this demo, this is LocalDB with the include MDF file. This would simulate your production code.

In the UnitTest project however, two very distinct ways to go about performing unit tests on the production code have been implemented.

LocalDB Instance w/In Memory Changes

The first implementation (RealUserTestsUsingLocalDbChangesInMemory) leverages the .rlinq designer model file of OpenAccess which contains all of the relationships (foreign key constraints) to dynamically generate a new database in LocalDB that mimics the production database structure and relationships. Additionally, through dependency injection and by overriding SaveChanges() of the OpenAccess context for the DemoModel, the implementation was changed to call FlushChanges(). An interface is not created for the DemoModel due to the nature of the autogenerated code that is produced by OpenAccess anytime a change is made to the .rlinq file. This allows us to fully leverage the constraints of the production SQL server without ever committing those changes to the database and persist the CRUD operations in memory which allows us to receive autoinc identity values on inserts. At the end of the test run during the tear down, the dynamically generated database is destroyed. By leveraging [SetUp] and [TearDown], you can create a unique instance of the database for each individual test and by using [TestFixtureSetUp] and [TestFixtureTearDown], you are able to create a single database instance for a group of tests with the caveat that you will likely need to control the order with which the tests execute. Most would agree that this is a “smell” and should be avoided but it is useful to have the flexibility available to you.

Business Logic Tests Using JustMock

The next set of tests (MockUserTests) takes a very different approach. Instead of creating an environment that simulates the real world production environment and then cleaning up after itself, we instead mock out the various objects and even lambda expressions so that we can test purely the business logic and disconnect ourselves from any external dependencies.  We still make use of [SetUp] but for the mocking tests it only serves to create the mock of the database model and instantiate a business logic handler object (production code) that we will call to invoke the various inserts, updates and deletes.

 

0
Nikesh
Top achievements
Rank 1
answered on 30 Oct 2012, 01:46 PM
Hi

James, any chance you could email this me nikeshp4tel@gmail.com.

Regards Nikesh
0
Kevin
Top achievements
Rank 2
answered on 05 Nov 2013, 06:56 PM
I know this post is pretty old, but it's exactly what I'm trying to achieve.  If you still have that sample around I'd like to get a copy.  (thetamage at comcast dot net).
0
Kristian Nikolov
Telerik team
answered on 07 Nov 2013, 02:37 PM
Hello Kevin,

Unfortunately we no longer have the project in question. As an alternative I can suggest you to post a resource request at our feedback portal. Voting for it would help us prioritize its eventual implementation. You can also check the Using the Context API  sample in our Samples Kit if you are interested in seeing unit tests against a live database.

If you have any questions or need assistance, do not hesitate to contact us via our Ticket System or post in our forums again.

Regards,
Kristian Nikolov
Telerik
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Tags
General Discussions
Asked by
Alexey
Top achievements
Rank 2
Answers by
Jan Blessenohl
Telerik team
Erik
Top achievements
Rank 1
Dimitar Kapitanov
Telerik team
Nikesh
Top achievements
Rank 1
James Legan
Top achievements
Rank 1
Ivailo
Telerik team
PetarP
Telerik team
Kevin
Top achievements
Rank 2
Kristian Nikolov
Telerik team
Share this question
or