Dotnet
Articles Part 2

Test GetArticles API Endpoint

PRO
Outline

Test: Get Articles

You're probably itching to see this run… I know I am. It's important to continuously test your software as you develop. So let's run the application to see if it runs successfully.

Wow, that's a bummer. A 500 Error is not good.

I actually did this on purpose. As software developers we need to be aware that errors will happen and we need to learn how to deal with them. Can you look at this error and determine what's wrong?

The big hint is here, here and here. Remember that we're using a Dependency Injection Service that automatically injects objects for us during instantiation. We need to tell the middleware how to map these objects for us and it'll do the rest of the work.

Let's fix it!

Dependency Injection Fix

For that we'll need to go into the Startup.cs file and head on down to the section where we add the injection services. We'll add the Articles Repository by mapping the interface to the implementation class.

Here's the code for the ConfigureServices method in the Startup.cs file (some code has been omitted for brevity): public void ConfigureServices(IServiceCollection services) { var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value); services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options =>…); services.AddDbContext(options =>…); services.AddAutoMapper(cfg => { cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); }); services.AddHttpContextAccessor(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddControllers(); services.Configure(options =>…); services.AddSwaggerGen(c =>…); }

Let's save it and run it again!

You'll see this time we get a 200 Success message. We don't have any Articles to list at the moment but it won't be long before we'll begin to see articles.

 

I finished! On to the next chapter