单元测试服务层 [英] unit testing service layer

查看:59
本文介绍了单元测试服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我是单位测试我的服务层:

我有一个名为CustomerService的服务层

  public   interface  ICustomerService 
{
IQueryable< Customer> GetCustomerByFilter(Expression< Func< Customer,bool>> where );
}
public class CustomerService:ICustomerService
{
private IRepository< Customer> customerRepository;

public CustomerService(ICustomerService rep)
{
customerRepository = rep;
}
public Customer IQueryable< Customer> GetCustomerByFilter(Expression< Func< Customer,bool>> where );
{
var customer = customerRepository.Get(Where));

if (customer == null
返回 null ;

返回客户;
}
}



存储库类如下:

  public   interface  IRepository< T> :IDisposable 其中 T: class  
{
T Get(Expression< Func< T,bool>>谓词);
}

public class 存储库< T> :IRepository< T> 其中 T: class
{
private ObjectContext上下文;
private IObjectSet< T>对象集;

public Repository()
this new demonEntities())
{
}

public 存储库(ObjectContext ctx)
{
context = ctx;
objectSet = context.CreateObjectSet< T>();
}

public T Get(表达式< Func< T,bool>>谓词)
{
T entity = objectSet.Where< T>(谓词).FirstOrDefault();

if (entity == null
返回 null ;

return objectSet.Where< T>(谓词).FirstOrDefault();
}



i写了一个单元测试如下:

 [TestMethod()] 
public void GetByFilter_ShouldReturn_ListofBroadSubjectArea_AfterAplyingFilters()
{
var customer = new 客户()
{
Id = 3
名称= Person1
StatusId = < span class =code-digit> 1
};

var list = CreateList();
var mockrepository = new Mock< ICustomerRepository>();
mockrepository.Setup(p = > p.GetAll())。返回(list.AsQueryable());
var service = new CustomerService(mockrepository。对象);

var filter = service.GetCustomerByFilter(l = > l.Name .Trim()。ToUpper()== customer.Name.Trim()。ToUpper()&& l.Id!= customer.Id)。ToList();

Assert.AreEqual( 1 ,filter.Count());
}
私人 静态列表<客户> CreateList()
{
var list = new 列表< Customer>()
{
new 客户{Id = 1 ,Name = Person1 StatusId = 1 },
};
返回列表;
}



ohk !!我的函数应该检查客户到使用模拟创建的列表...但它不是...断言应该给出一个真(表明存在相同的记录)在这种情况下,但显然它不....请帮助我在这个

解决方案

你好b $ b

我在过去的一小时里突然想到了它,终于找到了它。 :)



你只需要在你的web服务项目中创建一个default.aspx页面..!



以下是应该解决您的疑问的链接...

http://msdn.microsoft.com/en-us/library/ms243399(v = VS.90)的.aspx

hello i was unit testing my service layer :
I have a service layer named "CustomerService"

public interface ICustomerService
{
    IQueryable<Customer> GetCustomerByFilter(Expression<Func<Customer, bool>> where);
}
public class CustomerService : ICustomerService
{
    private IRepository<Customer> customerRepository;

    public CustomerService(ICustomerService rep)
    {
        customerRepository = rep;
    }
    public Customer IQueryable<Customer> GetCustomerByFilter(Expression<Func<Customer, bool>> where);
    {
        var customer = customerRepository.Get(Where));

        if (customer == null)
            return null;

        return customer;
    }
}


And the repository class is below:

public interface IRepository<T> : IDisposable where T : class
    {
        T Get(Expression<Func<T, bool>> predicate);
    }

    public class Repository<T> : IRepository<T> where T : class
    {
        private ObjectContext context;
        private IObjectSet<T> objectSet;

        public Repository()
            : this(new demonEntities())
        {
        }

        public Repository(ObjectContext ctx)
        {
            context = ctx;
            objectSet = context.CreateObjectSet<T>();
        }

        public T Get(Expression<Func<T, bool>> predicate)
        {
            T entity = objectSet.Where<T>(predicate).FirstOrDefault();

            if (entity == null)
                return null;

            return objectSet.Where<T>(predicate).FirstOrDefault();
        }


i wrote a unit test as follows:

               [TestMethod()]
        public void GetByFilter_ShouldReturn_ListofBroadSubjectArea_AfterAplyingFilters()
        {
            var customer = new Customer()
            {
                Id = 3
                Name = "Person1",
                StatusId = 1
            };

            var list = CreateList();
            var mockrepository = new Mock<ICustomerRepository>();
            mockrepository.Setup(p => p.GetAll()).Returns(list.AsQueryable());
            var service = new CustomerService(mockrepository.Object); 

            var filter = service.GetCustomerByFilter( l => l.Name.Trim().ToUpper() == customer.Name.Trim().ToUpper() && l.Id != customer.Id).ToList();

            Assert.AreEqual(1,filter.Count());
        }
        private static List<Customer> CreateList()
        {
            var list = new List<Customer>()
            {
                new Customer{Id = 1,Name ="Person1"StatusId=1},
            };
            return list;
}


ohk!! my function should check the "customer " to the list created using Mock...but its not...Assert should give a true(indicating that the same record exists) in this condition but apparently its not.... Please help me in this

解决方案

Hi
I was breaking my head over it for the past hour and finally found it. :)

U just have to create a default.aspx page in your web service project..!

following is the link that should solve your doubt...
http://msdn.microsoft.com/en-us/library/ms243399(v=vs.90).aspx


这篇关于单元测试服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆