Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MvcMiniProfiler require a web application to work, or can it be used in pure libraries, such as in unit tests?

I really like what I've seen regarding MvcMiniProfiler. I'd love to use the SQL monitoring features on non-web applications (windows services, unit tests, etc). Is this possible, or does it require an HTTP environment? The obvious difference would be results delivery, but if I can serialize the results to JSON or somesuch, that would be fine.

I threw together a simple unit test trying to get it to work, but MiniProfiler.Current appears to always be null, even after executing MiniProfiler.Start().

Am I barking up the wrong tree here?

like image 583
CoolUserName Avatar asked Dec 15 '11 21:12

CoolUserName


People also ask

How does unit test work?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.

What should be tested in unit testing?

The purpose of a unit test in software engineering is to verify the behavior of a relatively small piece of software, independently from other parts. Unit tests are narrow in scope, and allow us to cover all cases, ensuring that every single part works correctly.

How do you implement unit testing?

To implement unit tests, code must be written in a manner that supports it. To use unit tests, you are forced to evaluate how you write your code and how to make it more singular-accessible.

What makes code difficult for unit testing?

Tightly coupled code is extremely hard to unit test (and probably shouldn't be unit tested - it should be re-factored first), because by definition unit tests can only test a specific unit of something. All calls to databases or other components of the system should be avoided from Unit Tests because they violate this.


1 Answers

Excellent question. The core (the profiler instance and the connection wrappers) are not tied to http. If you can create your own instance, it should all work. However, all of the UI code is geared around web applications.

I know of people using it in WPF, winforms, etc - so it should work fine. However, you would also need to figure out a logical "unit of thing to me measured". In a web app it is obvious: a request.

Note that MiniProfiler.Current is purely a convenience (you could put an instance anywhere), but I'm pretty sure we made that possible to create your own abstraction (rather than http context).

(goes to check the code)

ah yes, here we go:

public static MiniProfiler Current
{
  get
  {
    MiniProfiler.Settings.EnsureProfilerProvider();
    return MiniProfiler.Settings.ProfilerProvider.GetCurrentProfiler();
  }
}

so you can provide your own ProfileProvider and then .Current should work.

like image 62
Marc Gravell Avatar answered Sep 29 '22 11:09

Marc Gravell