Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:17:25+00:00 2026-06-14T20:17:25+00:00

I’m trying to bring test layer to my project but I’m not getting there

  • 0

I’m trying to bring test layer to my project but I’m not getting there 🙁 hope someone can help me.

Controller (based on Automapper mapping and Dependency Injection Container):

    public virtual ActionResult SearchCategories(string keywords)
    {
        var result = _categoryService.SearchCategories(keywords);

        var resultViewModel = Mapper.
            Map<IList<SearchCategoriesDto>, 
                IList<SearchCategoriesViewModel>>(result);

        return View(resultViewModel);
    }    

Service Layer:

   public IList<SearchCategoriesDto> SearchCategories(String keywords)
    {
        // Find the keywords in the Keywords table
        var keywordQuery = _keywordRepository.Query;

        foreach (string keyword in splitKeywords)
        {
            keywordQuery = keywordQuery.Where(p => p.Name == keyword);
        }

        // Get the Categories from the Search
        var keywordAdCategoryQuery = _keywordAdCategoryRepository.Query;
        var categoryQuery = _categoryRepository.Query;

        var query = from k in keywordQuery
                    join kac in keywordAdCategoryQuery on k.Id equals kac.Keyword_Id
                    join c in categoryQuery on kac.Category_Id equals c.Id
                    select new SearchCategoriesDto
                    {
                        Id = c.Id,
                        Name = c.Name,
                        SearchCount = keywordAdCategoryQuery
                             .Where(s => s.Category_Id == c.Id)
                             .GroupBy(p => p.Ad_Id).Count(),
                        ListController = c.ListController,
                        ListAction = c.ListAction
                    };

        var searchResults = query.Distinct().ToList();

        return searchResults;
    }

Test maded but not working:

    [TestMethod]
    public void Home_SearchCategories_Test()
    {
        // Setup
        var catetoryService = new CategoryService(
                                   _categoryRepository, 
                                   _keywordRepository, 
                                   _keywordAdCategoryRepository);

        // Act
        var result = catetoryService.SearchCategories("audi");

        // Add verifications here
        Assert.IsTrue(result.Count > 0);
    }

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:17:26+00:00Added an answer on June 14, 2026 at 8:17 pm

    Solution to build an Integration test for a Service (in this case, Category Service), using Autofac, Automapper (not necessary in this Service but if it would be necessary, you would need to put in the TestInitialize method as you can see in the coment line in the following solution) and Entity Framework with Daniel J.G. help (thanks Daniel):

    First of all I created a separated Test Project using MSTest (only because there is a lot of documentation about it).

    Second you need to put the connection string for the Entity Framework where the test data is:

    <connectionStrings>
        <add name="DB" connectionString="Data Source=.\sqlexpress;Database=DBNAME;UID=DBUSER;pwd=DBPASSWORD;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    In the < configuration > section after the < / configSections >

    Third you create the class for the test:

    namespace Heelp.Tests
    {
        [TestClass]
        public class CategoryServiceIntegrationTest
        {
            // Respositories dependencies
            private IRepository<Category> _categoryRepository;
            private IRepository<Keyword> _keywordRepository;
            private IRepository<KeywordAdCategory> _keywordAdCategoryRepository;
    
            // Service under test: Category Service
            private CategoryService _categoryService;
    
            // Context under test: HeelpDB Connection String in app.config
            private HeelpDbContext db;
    
            [TestInitialize]
            public void InitializeBeforeRunningATest()
            {
                // IoC dependencies registrations
                AutofacConfig.RegisterDependencies();
    
                // HERE YOU CAN CALL THE AUTOMAPPER CONFIGURE METHOD
                // IN MY PROJECT I USE AutoMapperConfiguration.Configure();  
                // IT'S LOCATED IN THE App_Start FOLDER IN THE AutoMapperConfig.cs CLASS
                // CALLED FROM GLOBAL.ASAX Application_Start() METHOD
    
                // Database context initialization
                db = new HeelpDbContext();
    
                // Repositories initialization
                _categoryRepository = new Repository<Category>(db);
                _keywordRepository = new Repository<Keyword>(db);
                _keywordAdCategoryRepository = new Repository<KeywordAdCategory>(db); 
    
                // Service initialization
                _categoryService = new CategoryService(_categoryRepository,
                                                       _keywordRepository,
                                                       _keywordAdCategoryRepository);
            }
    
            [TestCleanup]
            public void CleanDatabaseResources()
            {
                // Release the Entity Framework Context for other tests that will create a fresh new context.
                // With this method, we will make sure that we have a fresh service and repositories instances on each test. 
                db.Dispose();
            }
    
            [TestMethod]
            public void Home_SearchCategories_Test()
            {
                // Arrange
                var keywords = "audi";
    
                // Act (the _categoryService instance was created in the initialize method)
                var result = _categoryService.SearchCategories(keywords);
    
                // Assert
                Assert.IsTrue(result.Count > 0);
            }
        }
    

    }

    Now you just have to run the test to see if it passes.

    To garantee integration tests, I would recomend a second database identical from the original/production database in terms of tables, but with only your test data.

    This will ensure that the tests results will remain the same based on your test data.

    The only drawback is that you will need to keep sincronized the tables, but you can use SQL Admin Studio Freeware Tool from Simego to achieve that.

    Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.