gpt4 book ai didi

unit-testing - 你在哪里设置 OrmLiteConfig.DialectProvider.NamingStrategy 在单元测试中?

转载 作者:行者123 更新时间:2023-12-04 04:46:03 25 4
gpt4 key购买 nike

我在一个项目中同时使用 ORMLite 和 Dapper,并且希望对两个 ORM 使用的命名约定进行标准化。为此,我想这样设置 NamingStrategy:

OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();

和一个单元测试来验证

public class BorrowerUnitTests : IDisposable    
{
private readonly ServiceStackHost appHost;

public BorrowerUnitTests()
{
//Set ORMLite to work with columns like ColumnLikeThis
// OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();

appHost = new BasicAppHost(typeof(BorrowerServices).Assembly)
{

ConfigureContainer = container =>
{

container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("LoanOrigination:Default"), PostgreSqlDialect.Provider));

container.RegisterAutoWiredAs<Repository, IRepository>();
container.RegisterAutoWired<BorrowerDomainService>();
}

}
.Init();
}

public void Dispose()
{
appHost.Dispose();
}

[Fact]
public void TestPostMethod()
{
var service = appHost.Container.Resolve<BorrowerServices>();

BorrowerCreate request = new BorrowerCreate();
request.FirstName = "Orm";
request.LastName = "Lite";
request.Email = "ormlite@servicestack.net";

var response = service.Post(request);

Assert.True(response.Id > 0, "Id retured from POST cannot be zero");
}

[Fact]
public void TestGetMethod()
{
var service = appHost.Container.Resolve<BorrowerServices>();

BorrowerGet request = new BorrowerGet();
request.Id = 1;

var response = service.Get(request);

Assert.Equal("ormlite@servicestack.net", response.Email);
}

[Fact]
public void TestPutMethod()
{
var service = appHost.Container.Resolve<BorrowerServices>();

BorrowerUpdate request = new BorrowerUpdate();
request.Id = 5;
request.FirstName = "MyFirstName2";
request.LastName = "MyLastName2";
request.Email = "MyEmail@Example.com";

var response = service.Put(request);

Assert.True(response.FirstName == "MyFirstName2", "FirstName is noth equal");
}
}

无论我将 NamingStrategy 语句放在哪里,我都会从 OrmLiteConfig 类的 DialectProvider 属性中得到一个异常,“您必须设置单例 'OrmLiteConfig.DialectProvider' 才能使用 OrmLiteWriteExtensions”

设置该属性的正确位置在哪里?

谢谢,斯蒂芬

最佳答案

您可以将它分配给您正在使用的 DialectProvider,例如:

PostgreSqlDialect.Provider.NamingStrategy = new OrmLiteNamingStrategyBase();

OrmLiteConfig.DialectProvider 是一个可以手动设置的单例:

OrmLiteConfig.DialectProvider = PostgreSqlDialect.Provider;
OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();

或者隐式地使用 new OrmLiteConnectionFactory() 构造函数,要运行它,需要从 IOC 解析:

container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(...));
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
OrmLiteConfig.DialectProvider.NamingStrategy = new OrmLiteNamingStrategyBase();
}

关于unit-testing - 你在哪里设置 OrmLiteConfig.DialectProvider.NamingStrategy 在单元测试中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25635527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com