Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi , I have this method :
C#
public static IEnumerable<W_H59MpxSessionFrais> ListeSessionFrais(MpxFraisContext db, int siteID, int stval, string codeFrais)
       {
           var sessionfrais = (from D1 in db.H59_MpxSessionFrais
                               where D1.CodeFrais == codeFrais
                               && D1.SiteId == siteID
                               && D1.StatusValidation == stval
                               select new W_H59MpxSessionFrais
                               {
                                   ID = D1.SessionFraisId,
                                   LibelleSession = D1.LibelleSession,
                                   DateDebutSession = D1.DateDebutSession,
                                   DateFinSession = D1.DateFinSession
                               });
           return sessionfrais.ToList();
       }



and i want to do test with Nunit but i don't know how to fill values to MpxFraisContext in the class test , if someone have an idea please tell me how to do the test class knowing that i'm working with asp.net MVC 4 ,Razor and linq to entities
Posted

You can write Unittest with both database data and Mock data. If you want to create a mock layer i recommend you implement the Repository Pattern and Dependency Injection.

The below example i have demonstrate a basic unit test with basic ASP.NET MVC Unitesting framework without using Dependency Injection
C#
[TestClass]
    public class YourControllerTest
    {
        private readonly SettingsController _settingsController = null;

        public SettingsControllerTest()
        {
            _settingsController=new SettingsController();
        }

        [TestMethod]
        public void ListeSessionFraisType_Of_IEnumerable_W_H59MpxSessionFrais()
        {
            var indexModel = _settingsController.ListeSessionFrais();
            Assert.IsInstanceOfType(indexModel, typeof(IEnumerable<W_H59MpxSessionFrais>));
        }
    }


Don't pass datacontext as a parameter. Please follow the Repository pattern.It will also abstract the datacontext away from the code and can test the business logic(Repositories) alone. You will get a lot code examples from the net for Repository Pattern
Hope this helps
 
Share this answer
 
i'm working with Nunit not MS test and i want to have test class for this method
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900