Unit test for insertedFact in optaplanner - unit-testing

How do we unit test rules in optaplanner that depend on insertedFact?
As per the examples for drools, its inserting fact using session object and then unit testing the actual rule. How can we access session object in optaplanner? I am creating SolverFactory using xml resource.

Take a look at ScoreVerifier, explained in the docs. It's part of optaplanner-test.jar.

Related

Mock a repository while testing an api controller

Im trying to get familiar with using MOQ and mocking in general. So I want to test an api controller which uses an assembly which serves as a repository for getting/updating data etc.
eg a structure like this.
HomeController
Index
Repository.GetSomeData (returns JSON object)
This repository class has an interface, and that is what is injected via the .net core startup class. The method in this case, GetSomeData does a number of steps via calls to the Db, as well as reading a file from the file system, parsing it and moving it off to another folder.
Question: How can a "mocked" Repository work without doing the things that the "real" object does? All the examples I see are simple addition, returning strings etc.
When you mock something like your repository, you going to stub out methods on the repository to return some canned result. Calls to those methods on the repository mock, then, bypass the real methods and instead just do what you've stubbed.
Essentially, you need to first identity what methods will be utilized. Then, you should determined appropriate responses those methods should return based on the particular scenario you're attempting to unit test. Then, you create the mock and add stubs for those methods with those responses.
The whole point of mocking is to remove variables, so you're intentionally trying to get to the "happy path": the set of internal responses that put the action in the state you need it to be in for specific test you're conducting.

Unit Testing basic Controllers

I have a number of simple controller classes that use Doctrine's entity manager to retrieve data and pass it to a view.
public function indexAction() {
$pages = $this->em->getRepository('Model_Page')->findAll();
$this->view->pages = $pages;
}
What exactly should we be testing here?
I could test routing on the action to ensure that's configured properly
I could potentially test that the appropriate view variables are being set, but this is cumbersome
The findAll() method should probably be in a repository layer which can be tested using mock data, but then this constitutes a different type of test and brings us back to the question of
What should we be testing as part of controller tests?
Controller holds core logic for your application. Though simple "index" controller actions don't have any specific functions, those that verify/actively use data and generate viewmodels have pretty much the most functionality of the system.
For example, consider login form. Whenever the login data is posted, controller should validate login/password and return: 1) to index page whenever logins are good. Show welcome,{user} text. 2) to the login page saying that login is not found in db. 3) to the login page saying that password is not found in db.
These three types of outputs make perfect test cases. You should validate that correct viewmodels/views are being sent back to the client with the appropriate actions.
You shouldn't look at a controller like at something mysterious. It's just another code piece, and it's tested as any other code - any complicated logic that gives business-value to the user should be tested.
Also, I'd recommend using acceptance testing with framework like Cucumber to generate meaningful test cases.
Probably the controller is the hardest thing to test, as it has many dependencies. In theory you should test it in full isolation, but as you already seen - it has no sense.
Probably you should start with functional or acceptance test. It tests your controller action in a whole. I agree with previous answer, that you should try acceptance testing tools. But Cucumber is for Ruby, for PHP you can try using Codeception. It makes tests simple and meaningful.
Also on a Codeception page there is an article on how to test sample controllers.

Is it possible to exclude Entity Framework Autogenerated Code from Code Coverage Statistics?

I have seen the [DebuggerNonUserCode] and [ExcludeFromCodeCoverage] attributes in resources and other SO questions about exlcuding code from coverage statistics, and wanted to know if it was possible to automatically add this attribute to the classes in the code generated by the Entity Framework using .NET 4.0.
Also would it need to be class level or could it be on the diagram.Designer.cs level, needing one attribute for all code generated by that diagram?
Since partial classes (which Entity Framework creates) merge attributes, extended functionality in other partial classes are also excluded if the attribute is class level in the template, it will have to be applied at the method level.
The best way that I've found to do this is using T4 (as recommended in #Craig Stuntz's answer) to:
include: using System.Diagnostics.CodeAnalysis; at the top of the file
Then apply [ExcludeFromCodeCoverage] to getters, setters and Factory methods by searching for:
#>get
#>set
Template_FactoryMethodComment
and placing them in the appropriate place.
This was made a lot easier using Tangible's T4 editor Extension for VS.
This is my first attempt and it seems to work, "your milage may vary", so complete a test run to make sure everything's working as necessary.

Entity framework unit testing with sqlite

Is it possible to unit test Entity Framework v2 repositories with SqLite? Is this only possible if my entities are plain Poco and not automatically generated by Entity Framework?
I've generated a entity model from SqlServer and in the generated .edmx file i found this in section SSDL content: Provider="System.Data.SqlClient". Correct me if I am wrong, but shouldnt that be System.Data.SQLite in order to work with sqlite?
Its only possible if two entity models are created (one for each provider) and then make both object contexts implement the same interface.
Its not worth the effort to manage two entity models.
I hope this get fixed in Entity Framework Ver 3.

Web Service Unit Testing

This is an interesting question I am sure a lot of people will benefit from knowing.
a typical web service will return a serialized complex data type for example:
<orgUnits>
<orgUnit>
<name>friendly name</orgUnit>
</orgUnit>
<orgUnit>
<name>friendly name</orgUnit>
</orgUnit>
</orgUnits>
The VS2008 unit testing seems to want to assert for an exact match of the return object, that is if the object (target and actual) are identical in terms of structure and content.
What I would like to do instead is assert only if the structure is fine, and no errors exist.
To perhaps simplify the matter, in the web service method, if any error occurs I throw a SOAPException.
1.Is there a way to test just based on the return status
2. Best case scenario would be to compare the doc trees for structural integrity of target and actual, and assert based on the structure being sound, and not the content.
Thanks in advance :)
I think that this is a duplicate of WSDL Testing
In that answer I suggested SoapUI as a good tool to use
An answer specific to your requirement would be to compare the serialized versions(to XML) of the object instead of the objects themselves.
Approach in your test case
Lets say you are expecting a return like expected.xml from your webservice .
Invoke service and get an actual Obj. Serialize that to an actual.xml.
Compare actual.xml and expected.xml by using a library like xmldiff(compares structural /value changes at XML level).
Based on the output of xmldiff determine whether the webservice passed the test.