我对单元测试有点新手。我有一个MVC5项目及其相关单元测试项目的解决方案。我在该解决方案中有4个其他类库,并添加了一个单元测试项目来封装其他类库的所有测试。
问题#1。以上是正确的方法还是每个库都需要自己的单元测试?
我创建了一个单元测试,基本上测试单元测试是否有效!
public class TestAppSettingsRetrieval
{
[TestMethod]
public void TestMethod1()
{
var emailProvider = Services.Helpers.ServicesAppSettings.EmailSettings.EmailProvider;
Assert.AreEqual(emailProvider, "gmail.com");
}
}
这会从我的服务项目中的app.config文件中提取(或应该提取)EmailProvider应用程序设置。
以下是appsettings配置和ServicesAppSettings类。
public static class ServicesAppSettings
{
public static class SmsSettings
{
public static string SmsProvider => ConfigurationManager.AppSettings["SmsProvider"];
public static string SmsAccountIdentifier => ConfigurationManager.AppSettings["SmsAccountIdentifier"];
public static string SmsPassword => ConfigurationManager.AppSettings["SmsAccountPassword"];
public static string SmsFromPhone => ConfigurationManager.AppSettings["SmsFromPhone"];
}
public static class EmailSettings
{
public static string EmailProvider => ConfigurationManager.AppSettings["EmailProvider"];
public static string EmailProviderHost => ConfigurationManager.AppSettings["EmailProviderHost"];
public static string EmailAccount => ConfigurationManager.AppSettings["EmailAccount"];
public static string EmailAccountPassword => ConfigurationManager.AppSettings["EmailAccountPassword"];
}
}
<appSettings>
<!--email settings-->
<add key="EmailProvider" value="gmail.com" />
<add key="EmailProviderHost" value="smtp.gmail.com" />
<add key="EmailAccount" value="**" />
<add key="EmailAccountPassword" value="**" />
</appSettings>
测试总是失败,因为返回的值为null。 问题#2我是否可以在单元测试中从另一个项目的app.config中检索?