creating dummy CreateShell method While Unit Testing - unit-testing

I want to Unit Test the view model and for that I am Inheriting Mefbootstraper class so Please suggest the method to override the CreateShell method inside Bootstrapper which returns dummy dependency object.

Based on your last comment, you could avoid creating a Shell and the entire Bootstrapper. So, instead of calling Bootstrapper's run() method, you can create mocks for RegionManager, Comnposition Container and ResourceManager in order to pass them through ViewModel's constructor.
This would not be an issue as you would load these mock instances with the minimum information needed for the test.
Furthermore, real Bootstrapper would not be needed because its functionalities would not be targeted for testing.
I hope this helps, Regards.

Related

How to mock Autofac IComponentContext.Resolve<IRepository>

I am looking how to mock autofac Resolve method. I am using NUnit and NSubstitute for unit tests.
I have a Business method which calls Repository method, and inside business method, i am using
_container.Resolve< IRepository>().GetData()==> here _container is IComponentContext.
Instead of calling real db, i want to mock repository method, but i am not able to do that, every time this line tries to call real db.
In my unit tests, I am trying to mock like this....
private IRepository _Repository;
_repository = Substitute.For< IRepository>();
_repository.GetData().Returns("abc");
Don't try to mock dependency injection. Instead of mocking DI, register your mock with Autofac.
This may mean that you need to modify your code so it allows you to add registrations to the container in a test scenario. In ASP.NET Core they have a notion of ConfigureTestServices for example that allows you to put registrations in a method that only get run in a test scenario.
Another way to do it is to use a custom Startup class where all the methods are virtual so you can override ConfigureServices or ConfigureContainer to register test overrides.
Unfortunately, exactly how you do this is so very application specific that there's not "one way to do it" or a "best practice." You're going to have to do some searching and looking at your own app to figure it out.
However, the takeaway here is: Don't try to mock DI. Use real DI with mocks inside the container.

Does django testing have a breakDown() similar to setUp() that is ran after all tests complete?

I am running some tests in django but they depend on a response from an outside service. For instance,
I might create a customer and wish to acknowledge it has been created in the outside service.
Once I am done with testing I want to remove any test customers from the outside service.
Ideally, there would be a method similar to setUp() that runs after all tests have completed.
Does anything like this exist?
You can make use of either unittest.TestCase.tearDown or unittest.TestCase.tearDownClass
tearDown(...) is the method gets called immediately after the test method has been called and the result recorded.
but, the tearDownClass(...) is gets called after tests in an individual class have run. That is, once per test class.
IMO, using tearDownClass(...) method is more appropriate since you may not need to check/acknoledge the external service after search test cases of the same class
So Django's testing framework uses a Python standard library module, unittest. This is where the setUp() method comes from.
This library contains another method tearDown() that is called immediately after the tests are run. More info can be found here

How do I correctly instantiate a new controller when using Ninject?

I created a demo Web API application that utilizes Ninject. The application works fine as I can run it, navigate to the defined route, and get the data I'm expecting. Now I want to begin adding unit tests to test the ApiController.
How do I instantiate a new ApiController? I'm using var sut = new DogsController(); but that results in an error, "... does not contain a constructor that take 0 arguments". It's correct I don't have a constructor that takes 0 arguments but Ninject should be taking care of that for me, correct? How do I resolve this?
You will have wired Ninject into the Web API application, not your unit test project. As a result, Ninject will not be creating the dependencies for your controller, or even your controller as you are explicitly creating it (in the Web API application, the framework creates your controller).
You could wire Ninject into your unit test project, but that would not be the correct thing to do. You should be creating your controller in your tests with a known state, so you should either be passing in known dependencies, or passing in some form of mock dependencies.
A DI container is not some piece of magic that transforms your code every time you write "new Something()". In your unit test you are newing up the controller by hand (which is good practice btw), but this means you will have to supply the constructor with the proper fake versions of the abstractions the constructor expects.

Overriding event closure on Grails GORM domain class for unit testing

I'm working on a fresh Grails project and recently noticed the default convention in the Spring Security Core generated User class now auto-encodes the password via a beforeInsert/Update event. That's a nice, clean, DRY way of doing the encode, and also makes it impossible to forget to do so.
However, now when trying to write up some unit tests which make use of said User class, I find I either have to mock out the springSecurityService (due to the encode), or more preferably (and cleanly), I'd just override the beforeInsert/Update closure with one that does nothing. Typically in Groovy one can override a method using the ExpandoMetaClass, ala...
User.metaClass.beforeInsert = { /* do nothing */ }
...but I'm finding that the original beforeInsert continues to be called upon creating and saving a new User. This in turn causes my unit tests to blow up. It's trivial for me to just work around this and mock out the service, but the above should work. Am I missing something? Is there something different with GORM's event closures that I'm not picking up on?
In order to improve performance Grails uses reflection directly with caches method handles for invoking events and not Groovy's meta layer. The reason is that if you are saving hundreds of domain instances it can seriously hurt performance if Grails had to go through Groovy's meta layer for each event.
There are ways around this such as defining your own User class that disables the event based on a system/environment property that your test sets etc. but there is currently no way to override the methods via meta programming.
The beforeInsert closure actually is not only a method like toString() or save(), but also it is a pre-defined event supported by Gorm. Override the method will not prevent Gorm from firing the PreInsert event, which leads to the original process.
If necessary, you can replace the code in the beforeInsert with a private method and then override the private method

How to unit test a fake implementation of a repository inside a service class?

I have an object which hold a message in many language this is called a LookupString in my project.
I have also a repository for this entity which is LookupTableRepository which is implemented from ILookupTableRepository.
I have a service class which is called LookupTableProvider.. this is simply to call out the repository. It only got one method which is GetEntry(string id, cultureinfo culture) which enable to get a message in the specified culture i want.
Now i want to unit test the LookupTableProvider which hold a repository.. but i wish to use a fake repository for this purpose since a moq won't help me much there since i only want to test the implementation of the provider.
What is the best way to make the unit test provider me an implementation of the fake repository if i use Ninject ? How i can call ninject to give me an instance of this fake repository ?
You shouldn't use an IoC Container for unit testing your classes. Simply new up your Provider and pass all mocks to the constructor.