如何从NUnit Test项目连接到数据库?

时间:2016-07-13 10:40:24

标签: unit-testing asp.net-mvc-4 testing mocking nunit

我是新的NUnit测试asp.net mvc web app,项目我想测试使用实体框架工作来连接数据库,从测试项目我需要连接到项目的同一数据库我想测试。

我正在测试的控制器如下所示。

public class EXController : Controller
{
    private readonly AppDbContext _context;
    private readonly CommonDbContext _commonContext;

    public EXController(CommonDbContext commonContext, AppDbContext context)
    {
        _commonContext = commonContext;
        _context = context;
    }

     public ActionResult Index()
    {
    // some simple code.
    }
}

我正在尝试编写NUnit测试,如下所示: 在测试项目的参考部分,我将测试中的Project添加为dll,我能够 访问我想测试的项目中的所有类。

CommonDbContext和AppDbContext是我在测试代码中使用的类,用于连接数据库,这些类将参数作为connectionstring。

所以我将连接字符串传递给这些类以连接来自Nunit测试项目的数据库。

我正在尝试使用以下连接字符串连接到NUnit测试项目中的数据库。

CommonDbContext objcdb = new CommonDbContext("metadata =\"res://*/Data.CommonDbModel.CommonDbModel.csdl|res://*/Data.CommonDbModel.CommonDbModel.ssdl|res://*/Data.CommonDbModel.CommonDbModel.msl\";provider=MySql.Data.MySqlClient;provider connection string=\"server=servername;database=databasename;integratedsecurity=False;user id=userid;password=password");

AppDbContext Objadb = new AppDbContext("metadata = \"res://*/Data.AppDbModel.AppDbModel.csdl|res://*/Data.AppDbModel.AppDbModel.ssdl|res://*/Data.AppDbModel.AppDbModel.msl\";provider=MySql.Data.MySqlClient;provider connection string=\"server=servername;database=databasename;integratedsecurity=False;user id=userid;password=password");

// creating instance for the contrller I want to test
ExController obController = new CLINController(objcdb, Objadb);

当我尝试从NUnit测试项目连接数据库时,我遇到异常。

Exception:
An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Format of the initialization string does not conform to specification starting at index 164.

**Stack Trace:**
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, String& keyname, String& keyvalue)
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions.ParseInternal(IDictionary`2 parsetable, String connectionString, IList`1 validKeywords)
   at System.Data.Entity.Core.EntityClient.Internal.DbConnectionOptions..ctor(String connectionString, IList`1 validKeywords)
   at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
   at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString)
   at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
   at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at BidMaster.Web.Infrastructure.CurrentUser.get_User() in D:\BWM_sprint3_newcopy\BidMaster.Web\BidMaster.Web\Infrastructure\CurrentUser.cs:line 30
   at BidMaster.Web.Controllers.CLINController.Index(Nullable`1 id) in D:\BWM_sprint3_newcopy\BidMaster.Web\BidMaster.Web\Controllers\CLINController.cs:line 51
   at BidMaster.Test.Controllers.CLINControllerTests.IndexTest() in D:\BWM_sprint3_newcopy\BidMaster.Test\BidMaster.Test\Controllers\CLINControllerTests.cs:line 47

我正在使用的连接字符串有什么问题吗?请帮帮我,请解释如何从Unit测试项目连接数据库。我是新手测试。

1 个答案:

答案 0 :(得分:0)

单元测试不应超过某个范围(单位,通常是一个类)。换句话说,你不应该触摸数据库 - 那已经是系统(集成测试)。

您的单元测试应该只测试Index方法中的逻辑(例如),同时模拟处理数据库调用的层。

沿着这些方向行事(使用RhinoMocks或其他模拟工具):

_commonDbContext = MockRepository.GenerateMock<CommonDbContext>();
_appDbContext = MockRepository.GenerateMock<AppDbContext>();

ExController obController = new CLINController(_commonDbContext, _appDbContext );

否则,如果您想要进入系统测试路径,则必须向您的服务所在的网址(端口号和所有)发出请求,而不是通过直接引用它。

只写了这个作为答案,因为评论太长了,如果你需要特定的答案,你需要提供有关你试图测试的方法的更多细节。

相关问题