Click here to Skip to main content
15,064,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to setup Unit Test in services using Moq and
NUnit I tried Mocking context and DbSet like
C#
var mockSet = new Mock<DbSet<Student>>();
var mockContext = new Mock<Context>();
mockContext.SetupGet(m => m.Student).Returns(mockSet.Object);
but my services depends on other service so I tried [Setup] in TestInit Method but when I'm trying to test service I'm getting an exception "invalid setup on a non-virtual (overridable in VB) member: m => m.Student""
I need a solution to register my services and unity in unit test

What I have tried:

C#
//this is how my whole code looks like
   [TestClass]
    public class StudentServiceTest
    {
        private IComService _mockComService;
        private IAccountService _mockAccountService;
       
        protected MRContext Context;

        private IUnitOfWork _mockUnitWork;

        [SetUp]
        public void TestInit(MRContext context, IAccountService accountService, IComService comService, IUnitOfWork unitOfWork)
        {
            
            this._mockComService = comService;
            _mockUnitWork = unitOfWork;
            this._mockAccountService = accountService;
            Context = context;

        }

        [TestInitialize]
        public void Initialize()
        {

            var mockSet = new Mock<DbSet<Student>>();
            var mockContext = new Mock<Context>();
            mockContext.SetupGet(m => m.Student).Returns(mockSet.Object);


           
        }


        [TestMethod]
        public void AutoAssignmentCases()
        {

            var service = new StudentService(_mockAccountService, _mockComService, _mockUnitWork);
            var students = service.DoSomeTask();
            Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsFalse(students.Any(c => !c.SubjectId.HasValue));
        }
    }
Posted
Updated 20-Apr-17 0:55am
v3

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