I am trying to unit test my application with mock data but is gives me this error
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I removed this error by seting the MockDataContext class constructor value to _dbSet = _context.User;
by doing this now I just test user dataset but I have to check all type of entities in my application.
below I attach my framework to test mock data repository
MockDataContext
using Atea.Azure.ApiManagement.Data;
using Atea.Azure.ApiManagement.Entities;
using Atea.Azure.ApiManagement.Framework.Data;
using Atea.Azure.ApiMangement.Tests;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Atea.Azure.ApiMangement.Tests
{
public class MockDataContext: DbContext, IDataContext
{
public MockDataContext()
{
this.Users = new TestUserDbSet();
}
public virtual DbSet<User> Users { get; set; }
int IDataContext.SaveChanges()
{
return 0;
}
}
}
MockdataRepository
using Atea.Azure.ApiManagement.Entities;
using Atea.Azure.ApiManagement.Framework.Data;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Atea.Azure.ApiMangement.Tests
{
public class MockRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly MockDataContext _context;
private readonly DbSet<TEntity> _dbSet;
public MockRepository(MockDataContext context)
{
_context = context;
_dbSet = _context.Set<TEntity>();
}
public void Delete(TEntity entity)
{
throw new NotImplementedException();
}
public void Delete(object id)
{
throw new NotImplementedException();
}
public TEntity Find(params object[] keyValues)
{
throw new NotImplementedException();
}
public void Insert(TEntity entity)
{
throw new NotImplementedException();
}
public void InsertRange(IEnumerable<TEntity> entities)
{
throw new NotImplementedException();
}
public IQueryable<TEntity> Queryable()
{
return (IQueryable<TEntity>) _dbSet;
}
public IQueryable<TEntity> SelectQuery(string query, params object[] parameters)
{
throw new NotImplementedException();
}
public void Update(TEntity entity)
{
}
}
}
MockUnitOfWork
using Atea.Azure.ApiManagement.Framework.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Atea.Azure.ApiManagement.Framework.Domain;
using System.Collections;
namespace Atea.Azure.ApiMangement.Tests
{
public class MockUnitOfWork : IUnitOfWork
{
IDataContext _dataContext;
private Hashtable _repositories;
public MockUnitOfWork(IDataContext dataContext)
{
_dataContext = dataContext;
}
public Result Commit()
{
return Result.Ok();
}
public void Dispose()
{
throw new NotImplementedException();
}
public IRepository<TEntity> Repository<TEntity>() where TEntity : class
{
if (_repositories == null)
_repositories = new Hashtable();
var type = typeof(TEntity).Name;
if (!_repositories.ContainsKey(type))
{
var repositoryType = typeof(MockRepository<>);
var repositoryInstance = Activator.CreateInstance(repositoryType.MakeGenericType(typeof(TEntity)),
_dataContext);
_repositories.Add(type, repositoryInstance);
}
return (IRepository<TEntity>)_repositories[type];
}
}
}
TestDbSet
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Atea.Azure.ApiMangement.Tests
{
public class TestDbSet<T> : DbSet<T>, IQueryable, IEnumerable<T>
where T : class
{
ObservableCollection<T> _data;
IQueryable _query;
public TestDbSet()
{
_data = new ObservableCollection<T>();
_query = _data.AsQueryable();
}
public override T Add(T item)
{
_data.Add(item);
return item;
}
public override T Remove(T item)
{
_data.Remove(item);
return item;
}
public override T Attach(T item)
{
_data.Add(item);
return item;
}
public override T Create()
{
return Activator.CreateInstance<T>();
}
public override TDerivedEntity Create<TDerivedEntity>()
{
return Activator.CreateInstance<TDerivedEntity>();
}
public override ObservableCollection<T> Local
{
get { return new ObservableCollection<T>(_data); }
}
Type IQueryable.ElementType
{
get { return _query.ElementType; }
}
System.Linq.Expressions.Expression IQueryable.Expression
{
get { return _query.Expression; }
}
IQueryProvider IQueryable.Provider
{
get { return _query.Provider; }
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _data.GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return _data.GetEnumerator();
}
}
}
TestUserDbSet
using Atea.Azure.ApiManagement.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Atea.Azure.ApiMangement.Tests
{
class TestUserDbSet : TestDbSet<User>
{
public override User Find(params object[] keyValues)
{
return this.SingleOrDefault(user => user.Id == (int)keyValues.Single());
}
}
}
testUser
using Atea.Azure.ApiManagement.Entities;
using Atea.Azure.ApiManagement.Framework.Data;
using Atea.Azure.ApiManagement.SDK;
using Atea.Azure.ApiMangement.Business;
using Atea.Azure.ApiMangement.Common.Logging;
using Azure_API_Delegation_Portal.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Results;
namespace Atea.Azure.ApiMangement.Tests
{
[TestClass]
public class TestUserController
{
[TestMethod]
public void TestGetUserByApimId()
{
var context = new MockDataContext();
context.Users.Add(GetUsers());
var userService = new UserService(new MockUnitOfWork(context), new EncryptionService(), new NLogger(typeof(User)));
var result = userService.GetUserById("1");
//Assert.IsNotNull(result);
Assert.AreEqual(1, result.Id);
}
User GetUsers()
{
return new User {
ApimId = "1",
Id = 1,
FirstName = "Ahmed",
LastName = "Jalal",
Email = "Donal@gmail.com",
Password = "password",
Salt = "9IKB+/KmPXDOAVmz6xJkIA==",
IsActive = true,
CreatedBy="Trump@gmail.com"
};
}
}
}
Aucun commentaire:
Enregistrer un commentaire