VS Test Project with Web API project (Telerik Data Access Service) - Telerik 2014.2 711

2 posts, 0 answers
  1. Sherman
    Sherman avatar
    21 posts
    Member since:
    Apr 2014

    Posted 06 Sep 2014 Link to this post

    Hi there, I'm currently using the latest version of Telerik for developing my app's Web API layer. I have managed to set up the Web API project. I've used the Telerik Data Access Domain Model for mapping with the tables from my database. And I have managed to successfully implement the Telerik Data Access Services.

    And now, when I want to start doing Unit Testing on my Web API, I find that I am having some challenges. My test class is called "MemberTypesControllerTest":

    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using DotNetWorkspacesAPI.Controllers;
    using DotNetWorkspacesAPI;
    using System.Linq;
    using Telerik.OpenAccess;
    using System.Web.Mvc;

    namespace DotNetWorkspacesAPI.Test
    {
    [TestClass]
    public class MemberTypesControllerTest
    {
    [TestMethod]
    public void GetAllMemberTypes ( )
    {
    MemberTypesController controller = new MemberTypesController();

    IQueryable<MEMBER_TYPERepository> memberTypes = controller.Get() as IQueryable<MEMBER_TYPERepository>;

    Assert.IsNotNull ( memberTypes );
    }
    }
    }

    When I run my test from the Visual Studio 2013 IDE, it tells me the following error:

    Test Name: GetAllMemberTypes
    Test FullName: DotNetWorkspacesAPI.Test.MemberTypesControllerTest.GetAllMemberTypes
    Test Source: c:\inetpub\wwwroot\DotNetWorkspaces\DotNetWorkspacesAPI.Test\MemberTypesControllerTest.cs : line 16
    Test Outcome: Failed
    Test Duration: 0:00:00.2287244

    Result Message:
    Test method DotNetWorkspacesAPI.Test.MemberTypesControllerTest.GetAllMemberTypes threw exception: 
    Telerik.OpenAccess.OpenAccessException: System.Configuration.ConfigurationErrorsException: A connection string was referenced by name 'DotNetWorkspaces' but could not be resolved through the web.config or the executables app.config file.
       at Telerik.OpenAccess.RT.Adonet2Generic.Impl.DBDriver.connect(ConnectionString connectionString, PropertySet driverProps, ConnectionPoolType poolType, LogEventStore pes)
       at OpenAccessRuntime.Relational.sql.SqlDriver.InitializeFor(ConnectionString connectionString, Boolean noConnect, PropertySet props, DBDriver& driver, Connection& conn, ConnectionPoolType poolType)
       at OpenAccessRuntime.Relational.RelationalStorageManagerFactory..ctor(StorageManagerFactoryBuilder b)
       at OpenAccessRuntime.storagemanager.StorageManagerFactoryBuilder.createSmfForURL() ---> System.Configuration.ConfigurationErrorsException: A connection string was referenced by name 'DotNetWorkspaces' but could not be resolved through the web.config or the executables app.config file.
    Result StackTrace:
    at Telerik.OpenAccess.RT.Adonet2Generic.Impl.DBDriver.connect(ConnectionString connectionString, PropertySet driverProps, ConnectionPoolType poolType, LogEventStore pes)
       at OpenAccessRuntime.Relational.sql.SqlDriver.InitializeFor(ConnectionString connectionString, Boolean noConnect, PropertySet props, DBDriver& driver, Connection& conn, ConnectionPoolType poolType)
       at OpenAccessRuntime.Relational.RelationalStorageManagerFactory..ctor(StorageManagerFactoryBuilder b)
       at OpenAccessRuntime.storagemanager.StorageManagerFactoryBuilder.createSmfForURL()
     --- End of inner exception stack trace ---
        at OpenAccessRuntime.ExceptionWrapper.Throw()
       at OpenAccessRuntime.storagemanager.StorageManagerFactoryBuilder.createSmfForURL()
       at OpenAccessRuntime.storagemanager.StorageManagerFactoryBuilder.createStorageManagerFactory()
       at OpenAccessRuntime.DataObjects.PersistenceManagerFactoryImp.createStorageManagerFactory()
       at OpenAccessRuntime.DataObjects.PersistenceManagerFactoryBase.init()
       at OpenAccessRuntime.DataObjects.PersistenceManagerFactoryImp.init()
       at OpenAccessRuntime.DataObjects.BootstrapPMF.getPersistenceManagerFactory(PropertySet props)
       at OpenAccessRuntime.Helper.getPersistenceManagerFactory(PropertySet props)
       at OpenAccessRuntime.DatabaseAdapter.AssertPersistenceManagerFactory(String usr, String password, Boolean open)
       at OpenAccessRuntime.DatabaseAdapter.GetObjectScope(TransactionProvider provider)
       at Telerik.OpenAccess.Database.GetObjectScope(TransactionProvider provider)
       at Telerik.OpenAccess.OpenAccessContextBase.GetScope()
       at Telerik.OpenAccess.OpenAccessContext.GetAllCore[T]()
       at Telerik.OpenAccess.OpenAccessContext.GetAll[T]()
       at DotNetWorkspacesAPI.OpenAccessBaseRepository`2.GetAll() in c:\inetpub\wwwroot\DotNetWorkspaces\DotNetWorkspacesAPI\Controllers\OpenAccessBaseRepository.cs:line 31
       at DotNetWorkspacesAPI.OpenAccessBaseApiController`2.Get() in c:\inetpub\wwwroot\DotNetWorkspaces\DotNetWorkspacesAPI\Controllers\OpenAccessBaseApiController.cs:line 20
       at DotNetWorkspacesAPI.Test.MemberTypesControllerTest.GetAllMemberTypes() in c:\inetpub\wwwroot\DotNetWorkspaces\DotNetWorkspacesAPI.Test\MemberTypesControllerTest.cs:line 19

    Here's what the "MemberTypesController.cs" file looks like (I didn't copy everything because its quite a lot, and I only want to stick to the method I am addressing here):

    // Code is generated by Telerik Data Access Service Wizard
    // using WebApiController.tt template

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web;
    using System.Web.Http;
    using DotNetWorkspacesAPI.Models;

    namespace DotNetWorkspacesAPI.Controllers
    {
        /// <summary>
        /// Web API Controller for MEMBER_TYPEs entity defined in DotNetWorkspacesAPI.MembersModel data model
        /// </summary>
    [RoutePrefix("MemberTypes")]
        public partial class MemberTypesController : OpenAccessBaseApiController<MEMBERS.MEMBER_TYPE, DotNetWorkspacesAPI.MembersModel>
        {
            /// <summary>
            /// Constructor used by the Web API infrastructure.
            /// </summary>
            public MemberTypesController()
            {
                this.repository = new MEMBER_TYPERepository();
            }

            /// <summary>
            /// Dependency Injection ready constructor.
            /// Usable also for unit testing.
            /// </summary>
            /// <remarks>Web API Infrastructure will ALWAYS use the default constructor!</remarks>
            /// <param name="repository">Repository instance of the specific type</param>
            public MemberTypesController(IOpenAccessBaseRepository<MEMBERS.MEMBER_TYPE , DotNetWorkspacesAPI.MembersModel> repository)
            {
                this.repository = repository;
            }

            // Get all method is implemented in the base class

            /// <summary>
            /// Gets single instance by it's primary key
            /// </summary>
            /// <param name="id">Primary key value to filter by</param>
            /// <returns>Entity instance if a matching entity is found</returns>
    [Route("{id:int}")]
    public virtual MEMBERS.MEMBER_TYPE Get(Int32 id)
            {
                MEMBERS.MEMBER_TYPE entity = repository.GetBy(m => m.MEMBER_TYPE_ID == id);

                if (entity == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                return entity;
            }

    Can someone help with this issue? What do I have to do to resolve this issue?
  2. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 10 Sep 2014 Link to this post

    Hi Sherman,

    Thank you for contacting us.

    The likely reason for the error you are experiencing is that the Unit Test project does not have the connection string required by the model defined in its App.config file.

    Generally the connection string required by the model to connect to the database is located in the .config file of the project which contains it. With this in mind we recommend adding the required connection string to the App.config of the Unit Test project (you may need to add the App.config file manually).

    Please note that if there are multiple connection strings defined in the .config file of the project containing the model you can see which one is required by checking the value of the connectionStringName field in the context class of your model (the one that derives from OpenAccessContext).

    I hope this helps. Should you have further questions, feel free to get back to us.

    Regards,
    Kristian Nikolov
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
Back to Top