【问题标题】:How to connect to Database from NUnit Test project?如何从 NUnit 测试项目连接到数据库?
【发布时间】:2016-07-13 10:40:23
【问题描述】:

我是 asp.net mvc web 应用程序的 NUnit 测试新手,我想使用实体框架连接数据库进行测试的项目,我需要从测试项目连接到我需要连接到项目的同一数据库想测试。

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

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 测试: 在测试项目的参考部分中,我将正在测试的项目添加为 dll,我能够 访问我要测试的项目中的所有类。

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

所以我将连接字符串传递给这些类以连接来自 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-testing asp.net-mvc-4 testing mocking nunit


    【解决方案1】:

    单元测试不应该超出某个范围(单元,通常是一个类)。换句话说,您不应该接触数据库——这已经是系统(集成测试)。

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

    类似的东西(使用RhinoMocks 或其他模拟工具):

    _commonDbContext = MockRepository.GenerateMock<CommonDbContext>();
    _appDbContext = MockRepository.GenerateMock<AppDbContext>();
    
    ExController obController = new CLINController(_commonDbContext, _appDbContext );
    

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

    仅将其写为答案,因为评论太长了,如果您需要具体答案,则需要提供有关您尝试测试的方法的更多详细信息。

    【讨论】:

    • 我需要从单元测试项目连接到我用于正在测试的项目的同一数据库,因为我的要求是测试存储过程返回的数据作为测试的一部分,如果我 MOCK 数据库我无法测试存储过程返回的数据,请帮助我如何从单元测试项目连接到我用于正在测试的项目的相同数据库模拟数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    相关资源
    最近更新 更多