NUnit and Sitefinity - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

NUnit and Sitefinity

  • NUnit and Sitefinity
  • Hello Sitefinity Community,

    I am a new comer to Sitefinity and have been trying to learn the in and outs of the product.  My issue is with working Test Driven, when I try to run my unit tests (NUnit) I get the following exception:

    An error occurred while analysing this project after it was built: System.NullReferenceException: Object reference not set to an instance of an object.
       at Telerik.Sitefinity.Configuration.Config.VerifySectionRegistered(Type sectionType)
       at Telerik.Sitefinity.Configuration.Config.GetSectionInternal(Type sectionType, Boolean safeMode)
       at Telerik.Sitefinity.Configuration.Config.GetSectionPrivate(Type sectionType, Boolean safeMode)
       at Telerik.Sitefinity.Configuration.Config.GetSectionPrivate[TSection](Boolean safeMode)
       at Telerik.Sitefinity.Security.PermissionAttribute.GetActionValue(String permissionSetName, String[] actions)
       at Telerik.Sitefinity.Security.PermissionAttribute..ctor(String permissionSetName, String[] actions)
       at Telerik.Sitefinity.Security.MethodPermissionAttribute..ctor(String permissionSetName, String[] actions)
       at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
       at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
       at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
       at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
       at System.Reflection.RuntimeMethodInfo.GetCustomAttributes(Boolean inherit)
       at NUnit.Core.Reflect.GetAttributes(ICustomAttributeProvider member, Boolean inherit)
       at NUnit.Core.Reflect.HasAttribute(ICustomAttributeProvider member, String attrName, Boolean inherit)
       at NUnit.Core.Reflect.HasMethodWithAttribute(Type fixtureType, String attributeName, Boolean inherit)
       at NUnit.Core.Builders.NUnitTestFixtureBuilder.CanBuildFrom(Type type)
       at NUnit.Core.Extensibility.SuiteBuilderCollection.CanBuildFrom(Type type)
       at NUnit.Core.TestFixtureBuilder.CanBuildFrom(Type type)
       at NUnit.Core.Builders.TestAssemblyBuilder.GetFixtures(Assembly assembly, String ns)
       at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, Boolean autoSuites)
       at NUnit.Core.Builders.TestAssemblyBuilder.Build(String assemblyName, String testName, Boolean autoSuites)
       at NUnit.Core.TestSuiteBuilder.Build(TestPackage package)
       at nCrunch.TestExecution.Frameworks.NUnit.NUnitDynamicTestFinder.FindFrameworkTestsUsingRuntimeInvoke(TestPackage package, ILogger logger, List`1 tests)
       at nCrunch.TestExecution.Frameworks.NUnit.NUnitTestFramework.FindFrameworkTestsInAssembly(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths)
       at nCrunch.TestExecution.TestFinder.FindTestsForFrameworks(ReflectedAssembly assembly, FilePath assemblyFilePath, IList`1 referencedAssemblyFilePaths, TestFrameworkDescription[] frameworks)
       at nCrunch.TestExecution.RemoteTaskRunner.AnalyseAssembly(TestFrameworkDescription[] applicableFrameworks)

    The tests are running in side of NCrunch, I also tried the NUnit test runner and the same error is thrown (with out the NCrunch part of the stack trace).

    I am sure it is a problem with some missing configuration but I can't work out what configuration to add into an App.Config for my testing project.

    Any direction would be great.


    Aaron Job
  • I'm seeing a similar stack trace. What are some possible reasons this error may be happening?

    [NullReferenceException: Object reference not set to an instance of an object.]
    Telerik.Sitefinity.Configuration.Config.VerifySectionRegistered(Type sectionType) +34
    Telerik.Sitefinity.Configuration.Config.GetSectionInternal(Type sectionType, Boolean safeMode) +21
    Telerik.Sitefinity.Configuration.Config.GetSectionPrivate(Boolean safeMode) +238
    Telerik.Sitefinity.Security.Claims.SessionCookieHandler.get_AuthenticationCookieTimeout() +59
    Telerik.Sitefinity.Security.Claims.SessionCookieHandler..ctor(CookieHandler innerHandler) +94
    Telerik.Sitefinity.Security.Claims.SitefinitySessionAuthenticationModule.InitializeModule(HttpApplication context) +143
    System.Web.HttpApplication.InitModulesCommon() +191
    System.Web.HttpApplication.InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) +1582
    System.Web.HttpApplicationFactory.GetNormalApplicationInstance(HttpContext context) +365
    System.Web.HttpApplicationFactory.GetApplicationInstance(HttpContext context) +200
    System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +591
  • Hello Bo and Aaron,

    I will try to help you with the issue(s) you are having. 
    The issue in short is happening because both of you are trying to create an integration test instead of unit test and you hit issue.

    Unit test usually is suppose to test one method only.. not to test the methods that this method uses and external properties. It is called Unit because it tests a single unit. Usually all services/managers/configs and everything is mocked or replaced with dummy implementations. They usually test the complexity of your methods and all outcomes that a method have with all incomes that a method can accept as parameters.

    Integration tests however usually test the whole system.. they do not test only an method but a method calling a method which reads configuration maybe applies security maybe stores something in the database or loads something and etc. this is your case. 

    You in short have a unit test that calls a method which internally calls a security manager or configuration and you get a null reference exception because in this small environment where the test is run you do not have a configuraiton, you do not have a full web application running you do not have database a request object and etc.

    My advice is mock everything which uses external systems and write unit tests.
    If you really want to make integration tests you need a full running Sitefinity instance.  So in order to achieve this you need to create a web service or web page for example which calls your tests (and not to use a runner from visual studio you need a web service or form or page which runs on top of Sitefinity ) and invoke the tests from it. Then you will have a real requests a real configurations, database connections and etc. and then like in every other integration tests you need to handle the clearing of the data which the test creates after each tests and etc.

    Nayden Gochev
    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
  • Thanks for the reply. I actually saw the stack trace while trying to simply connect to my server. I decided to discard the server and create a new one. I'm not seeing the problem anymore but unfortunately I don't know what exactly I did different this second time around that may have fixed it.
  • Thanks for the reply Nayden.

    I understand what the difference between a unit test and an integration test is.  The issue arises when I am trying to mock out the NewsDataProvider with nSubstitute so I can inject it
    into the service I am building.  I believe that NewsDataProvider as an abstract class so I thought I should be able to use nSubstitute to mock this dependency.

    If I am unable to mock NewsDataProvider please let me know as I could not find an interface that was suitable.

  • Hi All,

    I haven't used NUnit with Sitefinity. You are correct, NewsDataProvider is an abstract class (link here). You can mock the Predecessor of the NewsDataProvider, which is an abstract class as well, but it won't have lots of News specific methods, so this won't be useful to you.

    One idea to help you with this task is to create a wrapper, a class that inherits from NewsDataProvider and try to Mock it instead.

    Internally, we use JustMock for mocking Sitefinity entities like data providers.

    I can also offer another tool to support you TDD approach - it is the Sitefinity API Testing Tool - Please take a look at this video here.

    Please review and let us which option will make more sense to you.

    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
  • Thanks Peter,

    I think I will just create a facade to help with this problem, although wanted to avoid using one if I could.

    For anyone who wants to see what I have done I bogged about using facades to help you with unit testing a while ago:

    Here is the code for the Sitefinity NewsDataProvider: