More in this section

Forums / Developing with Sitefinity / Unit Testing the Fluent API

Unit Testing the Fluent API

7 posts, 0 answered
  1. stu
    stu avatar
    3 posts
    Registered:
    09 Sep 2011
    14 Sep 2011
    Link to this post
    I'd like to be able to run some simple integration tests and some console commands that will work with Sitefinity's Fluent API.  I'd like to be able to do this outside of the context of running the website on a browser.

    For example, it would be nice to create a Unit Test project in Visual Studio, add the appropriate dlls, and write a simple Integration/Unit Test against the Fluent API like this:

    [TestClass]
    public class RelationsMapperIntegrationTests
    {
        [TestMethod]
        public void MyUnitTest()
        {
            var t = App.WorkWith().BlogPosts().Get();
            Assert.IsTrue(t.Count() > 0);
        }
    }

    Is this possible?  The fluent API is preferred because it's robust and intuitive.
  2. Nikolay Datchev
    Nikolay Datchev avatar
    87 posts
    Registered:
    01 Nov 2016
    16 Sep 2011
    Link to this post
    Hello Stu,
    Setting up testing environment for unit testing our Fluent API is quite complex, e..g requires a lot of  complex "mocking". So integration test is the bet way to go in your case. The integration test though has to run on a installed and running instance of sitefinity , plus you need to be logged in with a user that has the permissions to execute your testing code - most of the time an admin user.

    The most simple way to go is to create some kind of Test Runner that uses reflection to enumerate the methods marked with the Test attribute and execute them again using reflection Invoke. The problem is that if all tests are run in one HTTP request - they usually affect each other and create dependency problems when run together.

    We are currently developing a test runner that sends a new request for each integration test , so they can run in maximum isolation. The framework is based on Mbunit Test attributes and Assert methods. We can provide you with this integration test runner framework once we are done, which should be pretty soon - the following 2 weeks. We will most probably include it also as part of the SDK.

    Regards,
    Nikolay Datchev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Bob
    Bob avatar
    25 posts
    Registered:
    14 Aug 2012
    14 Dec 2012
    Link to this post
    Hello,

    is this test runner already available?
    I'm in the same situation, I want to verify if some items (catalog) exists.

    If I execute the CatalogManager I get the same exception as in this blogpost: http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity/unit-test
    And to respond to the question from that post, no, my tests are in a separate project.

    What are the best practices / guidelines to do unit / integration testing?

    Kind regards,

    bob
  4. Pavel Iliev
    Pavel Iliev avatar
    140 posts
    Registered:
    18 Jul 2016
    15 Dec 2012
    Link to this post
    Hello Bob,

    Unfortunately due to changes in our priorities the test runner is not yet part of the offical release. As general guidelines:

    - Unit test you can use always when you don't want to test something in a real environment. Usually this is more or less algorithms, API tests, parsers. Something that has narrow and well defined scope. Whenver you have dependencies they should be mocked. Tests that cover small units of code fit well here. Those are uslually very fast since don't rely on databases, external services and so on. For unit tests the less you need to use mocking tools the better. Instead of calling a a complicated and difficult to instantiate type or a static type, you can provide a simple interface implementation that will provide results needed for the unit test to complete correctly.

    - Integraion tests - when you wish to confirm that something works in a real world environment - for example, you create a blog post then you want to see if the RSS feed has been updated, you should write integration test. Important thing here to have in mind is create atomic tests - the system should remain clean after each test. Because here you work with the real environment - database, web server, external services - everthing should be returned to its original state in order not to affect the remaining tests. Also it is good to execute each test in a new request and have in mind that they are slower and harder to maintain. If somethin fails the reason can be harder to be discovered - it can be everyhitng - from functionality bug to wrong database connection.

     

    Greetings,
    Pavel Iliev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Bob
    Bob avatar
    25 posts
    Registered:
    14 Aug 2012
    17 Dec 2012 in reply to Pavel Iliev
    Link to this post
    Hello,

    thank you for your reply, but there is where I'm stuck.
    I have an integration test for a custom web service to import products. (because I want to test the complete stack)
    In the test I call the service and afterwards I want to verify if the product is added without writing a dedicated operation like DoesProductExists.

    I can use the existing rest services but I was curious if it is possible to use directly CatalogManager in those test for simplicity. (and by adding a subset of the settings in eg app.config of the test project to activate the CatalogManager)

    Kind regards,

    bob
  6. Pavel Iliev
    Pavel Iliev avatar
    140 posts
    Registered:
    18 Jul 2016
    21 Dec 2012
    Link to this post
    Hello,

    Yes, you can use the all the managers directly. If you have a configured instance of the site then you just need some simple service or page to execute your tests. So your test should first call the service and then you can easily check the result with the manager. You can also use the manager to "clean up" the test data.

    All the best,
    Pavel Iliev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Bob
    Bob avatar
    25 posts
    Registered:
    14 Aug 2012
    21 Dec 2012 in reply to Pavel Iliev
    Link to this post
    Hello,

    but the unit tests are not included in my web application. They exists in a dedicated unit test project. So no setup site instance. I could use the existing rest services to do some checking and cleanup, but the CatalogManager seemed to be a more elegant solution, if I can enable it in an unit test project.

    Kind regards,

    bob
7 posts, 0 answered