Mocking WCF Services and dependecy to DB using Moq

137 views
Skip to first unread message

widia ahadi Putra

unread,
Jul 16, 2014, 7:02:02 AM7/16/14
to moq...@googlegroups.com

Hi,

Actually I have several project as follows:

- Data : Business object

- DataAccess: Access to database (using EF)

- Core : Core Business Logic

- Contract : Datacontract and Operationcontract for WCF Service

- Service : Service Implementation

Here are all my implementation example code

namespace Data
{
    public class Employee
    {
        public string Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public int Age { get; set; }
    }
}

namespace DataAccess
{
    public class EmployeeDBContext: DbContext
    {
        public EmployeeDBContext() : base("EmployeeDBContext") 
        {
        }
        public DbSet<Employee> Employee { get; set; }
    }
}

namespace Core
{
    public class EmployeeRepository : IEmployeeRepository
    {
        private EmployeeDBContext employeeDBContext;

        public EmployeeRepository()
        {
            employeeDBContext = new EmployeeDBContext();
        }

        public Employee GetEmployeeById (string id)
        {
            bool exist = employeeDBContext.Database.Exists();
            Employee emp = employeeDBContext.Employee
                        .Where(i => i.Id == id)
                        .SingleOrDefault();
            return emp;    
        }

    }
}

namespace Contract.Data
{
    [DataContract(Name="EmployeeDTO", Namespace= Namespaces.V1)]
    public class EmployeeDTO
    {
        [DataMember(IsRequired=true,Order = 1)]
        public string Id { get; set; }

        [DataMember(IsRequired = true, Order = 2)]
        public string FirstName { get; set; }

        [DataMember(IsRequired=true,Order = 3)]
        public string LastName { get; set; }

        [DataMember(IsRequired = true, Order = 4)]
        public int Age { get; set; }
    }
}
namespace Contract.Operation
{
    [ServiceContract(Namespace = Namespaces.V1)]
    public interface IEmployee
    {
        [OperationContract]
        EmployeeDTO GetEmployee(string id);
    }
}
namespace Service
{
    [ServiceBehavior(Namespace = Namespaces.V1)]
    public class EmployeeImpl : IEmployee
    {

        private IEmployeeRepository employeeRepository;

        public EmployeeImpl(IEmployeeRepository employeeRepository)
        {
            this.employeeRepository = employeeRepository;
        }
        public EmployeeDTO GetEmployee(string id)
        {
            Employee emp = employeeRepository.GetEmployeeById(id);
            EmployeeDTO eDto = Mapper.Map<Employee, EmployeeDTO>(emp);
            return eDto;
       
        }
    }
}

Now I am trying to create unit test to test my WCF Service which is it has dependency to database. GetEmployee is the method that I want to test. Because I am very new in unit testing and mocking, can anyone help me to give example using unit testing and mocking for EmployeeImpl below? For example given Id "1" then it should be return LastName "Putra". Many thanks

namespace Service.UnitTest
{
    [TestFixture]
    public class EmployeeImplTests
    {
        [Test]
        public void If_id_is_1_the_employee_name_shall_be_putra
        {
            //Can anyone please help me, what should I write here to mocking the webservice using Moq
        }
    }
}

Regards




Ross Jennings

unread,
Jul 17, 2014, 10:55:28 AM7/17/14
to moq...@googlegroups.com
​I think this might still be in Moq 3ish syntax, but it's just off the top of my head so you may want to tweak it a bit.

var mock = new Mock<IEmployeeRepository>(MockBehavior.Strict);

mock
.Setup(repo => repo.GetEmployeeById("1"))
.Returns(new Employee() 
LastName = "Putra" 
});

var service = new EmployeeImpl(mock.Object);
var result = service.GetEmployee("1");

Assert.AreEqual("Putra", result.LastName);

This tests that when the repository returns a result, the mapper successfully converts it to your EmployeeDTO with the LastName field intact. Not a really valuable test, but I suppose it's a good start.


--
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Moq Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moqdisc+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Not sent from my iPhone.
Reply all
Reply to author
Forward
0 new messages