Create a simple unit tests framework from scratch in Coldfusion - unit-testing

I know there are existing tools for testing a ColdFusion application (MXUnit, MockBox), but I'm creating a custom tool, so that it will require less configuration.
When I run a unit test file, it's done via a generic 'model' which retrieves all functions from the unit test file. Within each test function, I have to call assertEquals -- but these functions are in the model, so I cannot access them.
I tried by passing the model itself to the unit test file so it can call the models functions directly but it doesn't work and it adds logic to the test file, which I don't like.
I can also extend the model in the test file but I will have to call directly the test file, call super.init(this) so the model can fetch test functions, etc..
Is there a way to achieve this kind of process? What's the best option?

In answer to your question, it sounds like you want to inject variables / methods into the subject under test. You can do it like so:
myInstance["methodName"] = myFunction;
You can then call the injected method like so:
myInstance.myFunction();
Both MXUnit and TestBox use this technique.
Having said that I don't quite understand why you want to re-invent the wheel. TestBox is an excellent, proven testing framework which has a wealth of features which would take you an incredible amount of time to replicate. I'm not quite sure what the configuration issue you have could be - it really doesn't require very much setup. Maybe it might be worth asking how to setup and use TestBox rather than how to build your own testing solution :)
There is a good book (which is available in a free version) which you can read on TestBox here : http://testbox.ortusbooks.com/
Good luck!

Related

How to mock datacontext (LINQToSQL) which is part of another assembly?

I am using LinqToSQL in my project.As pert of my unit testing, I am trying to mock datacontext which is part of another assembly which is referenced in the ASP.NET WEB API project.
Based on the following URL:
http://weblogs.asp.net/rashid/100-unit-testable-linq-to-sql-repository
I tried to create a partial class for the datacontext but somehow it is not working.
Can anyone help me to know how to mock the datacontext class which is part of another referenced assembly.
Right from what I can gather from skim reading that article is that it's mostly call assertion and there are no tests around queryable results. IMO these tests are pretty meaningless and more over when you start putting in meaningful tests it wont work because Linq-to-sql is not mockable, DbContext is not mockable, can't use pocos, etc. This article was written in 2009 and Linq-to-sql is not a current product.
How big is the project? If at all possible move to EF6 and utilise either IDbSet to create a FakeDbSet or Mock the DbSets themselves and alot of the methods are virtual now. Have a look at this Q&A:
How are people unit testing with Entity Framework 6, should you bother?
If you search for Unit testing EF6 you find what your're trying to achieve much easier.
If you really can't change the Ling2Sql then use a façade pattern and compose you DbContext inside this. The façade would expose the DbSet are IQueryable and wrap up any method calls. This means though that you would have to create methods and properties that are virtual and then you can mock the façade. Alternatively you could also create an interface for the facade which you can also use for mocking. This would make any complex queries you write completely testable but you are creating code that calls other code. You would also have to watch the lifestyle of the façade as this is newing up the DbContext. This pattern is used to get round testing where AppSettings are involved, it's the same thing really. You just creating something that is mockable as a wrapper for something that isn't.

Unit testing real server data exchange?

I'm working on a project that has client and server side. And I'm writing a "pre-check-in" tool that will validate a lot of our communication between client and server.
I already have unit tests on both sides, now I really want to test the integration between both.
Like a real client connection to the server and vice-versa.
I really want to go unit testing on this, but I'm having a really hard time figuring out how I can initialize the MMVMCross framework and my view model classes.
In another thread I've asked for help on my "console app" that runs the tests but it is also really hard to initialize the framework and it makes me loose the coolness of unit testing with Visual Studio and reSharper.
My view models use SQLite and HttpClient with async/await.
For instance: I can't find a way to instantiate a view model that would need this interfaces:
IChatService, IMvxMessenger, IDataService, ISettingsService
Some from the framework some from my own code.
I known and I'm trying to register my ones, on a TestFixtureSetUp, but off course this fails, as the MVVM base subsystem (ioc?) is not setup yet.
Some above services, like IDataService for instance, also needs ISQLiteConnectionFactory, IMvxMessenger, ISettingsService.
I know unit testing is supposed to be fast, but my idea is to put all this tests in a new Category, that I would run only before my check-ins and my buddy, server developer, would run before his check-ins.
What would be the best approach here?
Any hint, suggestions, things to investigate/study would help at this point, as I'm practically stuck on this one.
Sergio
For instance: I can't find a way to instantiate a view model that would need this interfaces:
IChatService, IMvxMessenger, IDataService, ISettingsService
Generally this would be done using Mock implementations and then calling the ViewModel constructor directly with those Mocks.
I'm having a really hard time figuring out how I can initialize the MMVMCross framework
In order to initialise the IoC part of the framework you can use the MvxIoCSupportingTest helper class -https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Test/Cirrious.MvvmCross.Test.Core/MvxIoCSupportingTest.cs
If you then need additional parts of the framework, then generally you should mock these in some way. For example, see how navigation is mocked in these two articles:
http://blog.fire-development.com/2013/06/29/mvvmcross-enable-unit-testing/
http://slodge.blogspot.co.uk/2013/06/n29-testing-n1-days-of-mvvmcross.html
If it helps, an example of this type of test is https://github.com/slodge/MvvmCross-Tutorials/blob/master/Sample%20-%20TwitterSearch/TwitterSearch.Test/TwitterViewModelTest.cs#L21
If this answer doesn't have sufficient information, please provide a bit more example code about the tests you are trying to write.

Best Approach to verify public methods of a class have unit tests?

I have tried searching around the web and on SO, but haven't seen much discussion about t (or maybe I'm just not using the right keywords).
What I would like to do is write a script (or use a utility that already exists) to verify that a class or set of classes has unit tests written for them in the test project.
I've got a release coming up, and I want to make sure that all public methods of my business layer have unit tests. I'm trying to get everyone on board with TDD, but it hasn't happened yet.
I've got a pretty basic idea of how I would write a script to check this (open file, parse method signatures into some list, open corresponding test class file and check that each method in the list exists somewhere in the test file), but I wanted to see what other options are available.
.Net code coverage tools, such as NCover & dotCover, already exist. I would use one of those and read their reports.

phpUnit and models which inherit ADODB_Active_Record, stubbing ADODB_mysql

I've searched high and low and, astonishingly, cannot find an answer.
When trying to unit test my models with phpUnit how can I stub the database?
I'm using a PHP framework CMS which has a number of classes which inherit from a class ("Model") which inherits from ADODB_Active_Record. Model's constructor grabs a db object (from ADOConnection) and passes it to Active_Record's constructor.
It might not be Best Practice, but in order to change the code as little as possible, I'm thinking of tweaking grabDBObject() to return a stub object when in testing. That stub gets passed to ADODB_Active_Record, and, in theory, I can test my models.
However, I can't figure out how to create the stub. ADODB_Connection isn't simple. It's not going to be a matter of replacing Execute(). There are a bunch of other functions, like qstr(), that it appears I'll have to worry about and rewrite as necessary.
What's surprising is I can't find any discussions of people doing this. This has got to be a common problem. Am I going in the wrong direction? I understand that I can use something like dbUnit to actually do db queries rather than stubbing ADODB_connection, but I also understand that I should stub as much as possible, and that it's bad to rely on a db for unit tests of a model's methods.
So,
1. Should I be stubbing the db connection for unit testing?
2. How?
Have you tried the getMock() method of phpunit ?
It allows you to provide a fake object for testing, without touching your production code.
But in that case, you have to build the expected result by yourself.
The doc is here :
http://www.phpunit.de/manual/3.0/en/mock-objects.html

Django test fixtures: How to cope with test data involving time?

Currently I am doing TDD on a reusable Django app. One test should make sure that my view only returns articles that have a publish-date that is in the past. I am quite new to testing in Django. So far I have learned how to use .json files as fixtures for my test classes.
However for this test I do not want to insert publish dates that are 1000 years in the future. After all another species might discover our ancient internet, check out my source and wonder why my test fails :) What other approaches are there to solve this problem? Static .json files seem to be a bit hard to maintain as well as the application grows... mocking the datetime.datetime.now() method in my tests feels tedious as well.
There must be an easy way to produce the .json fixture on the fly before the test runs and always have 2 days from now as the publish-date for some of my entries...
You could try subclassing the datetime functions (see "Using Mock objects in Django for testing the current date").
Mocking datetime was my first thought as well but as LaundroMat pointet out in his blog post himself, this is rather hacky and bad.
I came up with another solution:
I just gave up on .json fixtures. I don't even know why Django encourages to use them. They look ugly and they are impossible to maintain.
Instead I added a module test_data.py to my fixtures folder. That module imports my models and defines some nice methods that create test-data for me. In my tests.py I dropped the fixtures = ['some_json_file'] line and added a setUp() method instead. This method executes my 'dynamic fixtures' from my test_data.py module.
This is so simple and obvious that I wonder if there is anything wrong with this approach. If no one comments on this solution, I will mark is as accepted in a couple of weeks or so...
You can try using django dynamic fixture, it will fill date/time properties automatically for you.
Creating test data
Indeed it is far more flexible to create your test data with Python.
There are several packages that support such things, for instance
model-backery
django-dynamic-fixture
These two use fairly different approaches. Have a look and pick the one you like more.
You can also write data generation functions from scratch. If you partition your logic carefully, this is not overly cumbersome.
Handling time
For tests involving time, the right approach is indeed mocking -- but don't do it yourself, use a nice library. See for instance
freezegun
There is even a pytest plugin for it.