Saturday, July 21, 2018

Help with ASP.NET Core Razor Pages, EF Core, Architecture/Design and Unit Testing

I'm a junior dev and I am pretty lost and overwhelmed with a range of topics that all fit together into a bigger picture. I want to start automating testing of my applications for a multitude of reasons. I just feel like something hasn't clicked.

To preface the description of my problem, I would like to say: I have been searching and reading many resources trying to piece together my understanding over the past few weeks. I would also like to mention that I work at a small shop (just me and my boss, but many projects) and while my boss is a great programmer and mentor, they still use WebForms and do not use ORMs or Unit Testing so I can't exactly ask them these questions.

I have read over the example source code here

https://github.com/aspnet/Docs/tree/master/aspnetcore/test/razor-pages-tests/samples/2.x

But I just can't wrap my head around the way that the data access is structured, or rather how to apply it to a larger application. I'm trying to figure out what pattern I should follow with this (repository?) if any. Even in the source code I linked, the authors have methods in the DbContext that seem to defeat the purpose of using EF. I don't always want to get all the objects of an entity type. I might want to filter/query the DbSet, so how would I unit test that?

This article argues that EF Core is a Repository pattern and you shouldn't put an abstraction layer over it.

https://www.thereformedprogrammer.net/is-the-repository-pattern-useful-with-entity-framework-core/

Alright great, but how do I test more complex things? Should I even unit test my interactions with EF? For instance, my page model methods are really fat right now. I want to refactor the code into more testable units, but I'm not sure how.

Right now, my ~20 unit tests feel pretty useless. The pagemodel tests I have written are either super simple "Method_ReturnsNotFound_WhenIDParamIsNull" or much more difficult to mock thanks to having something like an Identity usermanager dependency.

I really want to be better at what I'm doing. I was hoping someone could better explain to me where to go from here or just give advice.

TL;DR: I have bitten off more than I can chew and need help.

Thanks in advance and sorry for the wall of text.

Help with ASP.NET Core Razor Pages, EF Core, Architecture/Design and Unit Testing Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team