我有一个使用NUnit运行单元测试的单独测试项目的webappliction.当我的测试项目试图发现测试时,我遇到以下异常:
An exception occurred while test discoverer 'NUnit3TestDiscoverer' was loading tests. Exception: Could not load file or assembly 'nunit.engine, Version=3.7.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb' or one of its dependencies. The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)
这当然是一个非常常见的错误,但是我无法在任何地方找到对此特定程序集的引用,或者其他依赖项实际上导致了该问题.另一件事是我目前已经3.9.0安装了NUnit而不是上面提到的3.7.0.我已经尝试清理和重建解决方案并恢复所有Nuget包并清除obj目录也没有帮助.
我有一个 Data.Migrations 项目,它将运行任何实体框架迁移来更新数据库模型。最近我已将此项目更新到.NET 6,并Program.cs使用以下代码添加了一个记录器:
var serviceCollection = new ServiceCollection();
var serviceProvider = serviceCollection.BuildServiceProvider();
_logger = serviceProvider.GetService<ILogger<Program>>();
Run Code Online (Sandbox Code Playgroud)
然而,这会导致_logger == null.
如何将记录器添加到 Program.cs 中?
在Typescript中,我有一个Contact实现接口的类IContact.我想在类中添加一个属性或函数,Contact它返回另外两个字段(firstname和lastname)的组合.IContact界面的定义如下:
export interface IContact {
firstname: string;
lastname: string;
}
Run Code Online (Sandbox Code Playgroud)
这个Contact类看起来像这样:
export class Contact implements IContact {
constructor(
public firstname: string,
public lastname: string
) { }
public fullname(): string {
return this.firstname + " " + this.lastname;
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我想输出结果,Contact.fullname()但我收到的错误fullname不是函数.我可以访问该类的所有其他属性.
====更新===
我会添加一些额外的代码来澄清一些东西.当我fullname在我的视图中输出属性时,我已尝试contact.fullname()但也contact.fullname没有结果.在我的组件中,试图弄清楚发生了什么,我试图fullname像这样输出到控制台:console.log("FULLNAME " +contact.fullname());,但是在控制台中给我以下消息:EXCEPTION _this.contact.fullname is not a function
=====更新答案========
正如Sakuto所说,联系人列表是由服务器收到的一些json创建的.通过调用它的构造函数完全创建一个新实例,我能够输出fullname属性.谢谢Sakuto!
我在Angular 2中创建了一个表单,用户可以使用该表单编辑SearchProfile可以从列表中选择的表单。最初,一切正常,但是当我从SearchProfiles 列表中选择其他项目时,会收到以下异常:
There is no FormControl instance attached to form control element with name: controlName。整个事情由3个部分组成:2层的组件SelectProfileComponent,SearchProfileComponent以及服务ProfileService。ProfileService包含ActiveProfile已选择SearchProfile进行编辑的。
ProfileService看起来像这样:
@Injectable()
export class ProfileService {
private activeProfileSource = new ReplaySubject<ISearchProfile>();
activeProfile$ = this.activeProfileSource.asObservable();
constructor(private http: Http) {
this.selectProfile();
}
public getSearchProfile(profileId: number = null): Observable<ISearchProfile> {
let url = `<url>?profileid=${profileId}`;
this.http.get(url).map((result) => {
return <ISearchProfile>.result.json();
});
}
public selectProfile(profileId: number = null) {
this.getSearchProfile(profileId).subscribe((p) => …Run Code Online (Sandbox Code Playgroud) 我使用 .Net 5 创建了一个微服务,它有一些只能使用 jwtBearertoken 调用的端点。
类中的 和 方法如下ConfigureServices所示:ConfigureStartUp
public void ConfigureServices(IServiceCollection services)
{
ConfigureDatabaseServices(services);
ConfigureMyProjectClasses(services);
services.AddVersioning();
services.AddControllers();
services.AddAuthentication(_configuration);
// Add framework services.
var mvcBuilder = services
.AddMvc()
.AddControllersAsServices();
ConfigureJsonSerializer(mvcBuilder);
}
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment webEnv,
ILoggerFactory loggerFactory,
IHostApplicationLifetime applicationLifetime)
{
_logger = loggerFactory.CreateLogger("Startup");
try
{
app.Use(async (context, next) =>
{
var correlationId = Guid.NewGuid();
System.Diagnostics.Trace.CorrelationManager.ActivityId = correlationId;
context.Response.Headers.Add("X-Correlation-ID", correlationId.ToString());
await next();
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
applicationLifetime.ApplicationStopped.Register(() =>
{ …Run Code Online (Sandbox Code Playgroud) 我有一个Controller类TodoRepository通过DI实现一个接口的类ITodoRepository.
界面:
public interface ITodoRepository
{
public bool ValidateTodo(Todo todo);
}
Run Code Online (Sandbox Code Playgroud)
存储库类:
public class TodoRepository : ITodoRepository
{
public bool ValidateTodo(Todo todo)
{
//some validation
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
控制者:
public TodoController : BaseController
{
private readonly ITodoRepository _todoRepository;
private const string INVALID_TODO_MESSAGE = "Invalid todo.";
public TodoController(ITodoRepository todoRepository)
{
_todoRepository = todoRepository;
}
public IActionResult Post(Todo todo)
{
if(!_todoRepository.ValidateTodo(todo))
{
return new JsonResult(INVALID_TODO_MESSAGE);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了能够INVALID_TODO_MESSAGE在每一个Controller使用TodoRepository我想要移动它的东西中使用 …
我有一个MyClass要实例化的类,该类包含一个私有属性ObjectRepository,该私有属性在MyClass调用的构造函数时要实例化。
public MyClass
{
private ObjectRepository _objectRepository;
public MyClass()
{
}
}
Run Code Online (Sandbox Code Playgroud)
MyClass 实例化以添加到列表中:
objects.Add(new MyClass());
Run Code Online (Sandbox Code Playgroud)
遵循SOLID原则,最干净的方法是什么?
- - 编辑 - -
我还忘了提到的是,当我编写单元测试时,MyClass我希望能够进行模拟ObjectRepository。ObjectRepository从的构造函数实例化时,这是不可能的MyClass,因为测试中没有对的引用_objectRepository。
我有一个非常简单的片段代码,其中包含一个foreach循环:
foreach(var item in list) {
var valueForPropertyA = getPropertyAValue(item?.Id ?? 0);
if(valueForPropertyA == null) {
continue;
}
item.PropertyA = new PropertyADto(valueForPropertyA);
}
Run Code Online (Sandbox Code Playgroud)
我们也有一些自动代码检查工具,它给了我下面的警告在上面的代码:
'item' is null on at least one execution path
有没有可能是一个item在list为null还是我误解的警告?