FitNesse tests with objects as parameters - c#

I am having trouble writing a FitNesse test for my application.
The test in itself is pretty simple.
We setup a loan, it has a few properies (Principal, Interest, Fees).
There is another object, Rules, which contains properties for paying back the loan.
The Loan class has a method called SplitLoan hat takes a Rules object, and will return a list of payments that need to be made.
A Payment would contain a total Amount, as well as individual amounts for Principal, Interest, and Fees.
How do I write this as a test in FitNesse?
I can't even get the call to happen because I don't know how I setup the Rules object that gets passed into the SplitLoan function.

I'm not positive this works in C#, but I know SLIM supports putting an object reference into a Symbol. You could use one fixture to build the Rules object, get a reference to it, then pass the symbol into another fixture that needs it as input.
http://fitnesse.org/FitNesse.SuiteAcceptanceTests.SuiteSlimTests.SlimSymbolCanHoldInstanceUsedAsParameter
Alternately, if you don't want to (or can't) get an object reference and store it in a symbol, you could have a fixture that builds the rules and stores them in a singleton with an identifier. Then you could pass that identifier in later fixtures and the fixture code could fetch the rules object from the singleton.

Does something like this do what you want? This is a fitSharp test.
|name|myrules|with|new|rules|
|with|myrules|
|set|myproperty|123|
|with|new|loan|
|set|principal|1000|
|set|interest|5|
|split loan|<<myrules|
|total|interest|principal|fees|
|100|80|5|15|
|100|78|7|15|

Related

Is there a way to set Allure attributes dynamically?

In my testing scenario, I've set up a few parameterized tests in order to test various data inputs to a function. I'm also using Allure to generate my test report. My issue is that if I set Allure attributes for things like suite and sub suite for instance in the test class itself, it shows the same static data for every test entry in the report. What I'd like to do is to have the sub suite attribute read something like "Test data for input " and then either input 1, 2, 3, etc., depending on which input is being ran through at the time. The input object does contain an index number property, so getting the specific input number isn't an issue, I just need a way to write that to the Allure attribute.
The C# based allure implementation I'm using does have an AllureLifecycle class, which has in instance property (Which from what I understand should give access to the currently running test instance), and then a UpdateTestCase method. What I'm not sure of though is whether the suite and attributes like it can even be updated using this method, or if they are required to be set via data annotations.
What I guess I'm trying to figure out is if what I want to do is even possible? Am I on the right track here?

C# - Can a Method Examine Another Method's Use of a Parameter?

I have a variety of methods that use a configuration object to fill in placeholders in a template. Different methods use different subsets of properties of the configuration object. I'd like an easy way to check that all the properties a given method uses are present in a given config object.
Right now I have a method like this:
private static void ValidateConfiguration(CustomerConfiguration config, params string[] properties)
This has the maintenance disadvantage that it relies on a separate set of strings for the properties used. What I'd love to do is have the validation method look at the calling method and see what properties of the config object are being accessed. Can this be done?
(I could also wrap String.Replace() in a method that checks for nulls, but that's less fun.)
A type safe way to handle your problem would be to implement several interfaces with different meaningful subsets of properties. My understanding is that the presence/absence of the properties in your case depends on the type of configuration object and is dynamic.
you could use a signature like that
ValidateConfiguration<T>(CustomerConfiguration config)
where T represent the interface and use reflection to list the required properties. While it would be practically impossible to parse the code of a method to infer its usages of a data structure, reflection on types (to extract properties) is fairly easy.
Different methods use different subsets of properties of the configuration object.
If you're only creating one instance of the configuration property, then the properties it needs to have are whichever ones are going to be used by any method. In other words, if at least one method needs that property, then the object needs that property.
In that case there's no need to validate it in relation to individual methods that need it. All of its properties need to be populated because they're all needed somewhere. If they're not needed anywhere, you can delete them.
Then, instead of validating that object based on the needs of a particular method, you validate it once, perhaps at startup. All of the properties are needed, so if they haven't been specified then the application just can't run. (Sometimes it's good to include defaults in your configuration object. You might have one property that you want to be able to configure, but in real life it's never going to change.)
If you're creating different instances of the same object for use in different methods and you only want to populate certain properties then it's better not to do that. Just create more granular objects for different scenarios containing all the properties you need.
What frequently happens is this: We have an object with lots of properties and we only use a few of them, so we populate those properties and pass the object to a method. The other properties are null.
Then, someone modifying that method decides that they need another property, so they try to use it, and they're surprised to find out that it's null. Then they have to go back and trace where that object was created and figure out what is populated or not. That's confusing and time-consuming.
Unless fields are entirely optional and it doesn't matter whether they are populated or not, we don't want to find ourselves looking at an object with lots of properties and guessing which ones have been populated because individual methods that create the object "know" which properties other classes do or don't need.

Any way to get the definition of a property from the code itself?

public abstract class Unit
{
public abstract List<Move> allowedMoves{get;}
}
public class Javelineer : Unit
{
public List<Move> allowedMoves =>
new List<Move> {Move.Impale, Move.JavelinThrow, Move.ShieldBlock};
}
public class Dragon : Unit
{
public List<Move> allowedMoves =>
new List<Move> {Move.BreatheFire, Move.Swipe, Move.Bite, Move.Devour, Move.TailBash};
}
The X:
Given the above code, if and how can I retrieve the allowed moves of a given unit without necessarily instantiating a new object?
I know I can retrieve the property with this code:
typeof(Javelineer).GetProperty("allowedMoves")
But if and how can I retrieve the definition of this property?
The Y:
The client (web browser) must send the game server the player's unit. This includes the unit's type and moves this unit is able to perform (4 out of all available; similarily to Pokemon).
While the validation (of course) is performed on the server, the browser still needs to get a list of available unit types and allowed moves.
In order not to duplicate code, I would like to avoid hard-coding this data in Javascript.
Having read some excellent SO questions & answers I think I can retrieve all available units with code similar to this:
Assembly.GetExecutingAssembly().GetTypes().Where(
type => type.BaseType == typeof(Unit)
).Select(type => type.Name).ToList()
I'd call this code on server startup, cache the result and send the cached result to every connecting client, because I have feeling this code is likely expensive to call.
But how can I retrieve the list of allowed moves?
You have a couple of options, but TL;DR: Construct the object instance and read the property.
In any case, here are some options, creative minds might be able to find a couple more even.
Construct the instance, read the property.
This is your best option code-wise because it will be easy to understand, maintain, bugfix.
Rewrite the code to allow for easy detection of the values using reflection
One way to do this would be to use attributes, tagging the property or object with the legal moves. However, to avoid having the bug that the attributes does one thing, the code another, you might have to change the code to use the attributes as well, which would be a performance hit.
Additionally, reading those attributes would likely construct many more objects than your original object.
Use mono.cecil or some other IL-inspection library to decode the code of the property getter and finding the construction of that list, extracting the values being added to the list. You would essentially either have to dumb down the code of that property to be on par with what you have right now (and never allow it to become more complex) or basically simulate execution of code.
This is like constructing a flotilla of space warships with enough firepower to demolish a local starsystem, just to kill an ant.
Bottom line, construct the object instance, read the property.

Which of the following methods should I write unit tests for?

I'm looking at unit tests for the first time.
As I'm in Visual Studio 2008 I started with the build in testing framework.
I've hit the button and started looking at filling in the blanks, it all seems fairly simple.
Except, I can see two problems.
1) A lot of the blank unit tests seem to be redundant, is there a rule of thumb for choosing which methods not to write unit tests for.
2) Is there a best practise for writing tests for methods that read/write a database (SQL Server in this case)
I'll give an example for (1).
I'm writing unit tests for a WCF web service. We use wscf.blue to write our web service WSDL/XSD first.
Here's the path through the (heavily simplified) code for the methods which consumes a list of Users and writes them to the Users table in the database.
Entry Point
|
|
V
void PutOperators(PutOperatorsRequest request) (This method is auto generated code)
|
|
V
void PutOperatorsImplementation(PutOperatorsRequest input) (Creates a data context and a transaction, top level exception handling)
|
|
V
void PutEntities<T>(IEnumerable<T> input) (Generic method for putting a set of entities into the database, just a for loop, T is Operator in this case)
|
|
V
U PutEntity<T, U>(T entity) (Generic Method for converting the input to what the database expects and adding it to the DataContext ready for submission, T is Operator, U is the data layer entity, called User)
|
|
V
(This method calls 3 methods, first 2 of which are methods belonging to "entity" passed into this method, the 3rd is an abstract method that, when overridden, knows how to consume a BL entity and flatten it to a database row)
void EnsureIDPresent() (Ensures that incoming entity has a unique ID, or creates one)
void ValidateForInsert(AllOperators) (Does this ID already exists, etc)
User ToDataEntity(Operator entity) (simple field mapping excersice, User.Name = Operator.Name, etc)
So, as far as I can tell I have 3 methods that do something obviously testable:
EnsureIDPresent() - This method takes and input and modifies it in an easily testable way
ValidateForInsert() - This method takes the input and throws exceptions if criteria are not met
ToDataEntity() - This method takes an input, creates a data row entity and populates the values. Should be very easy to test.
There is also:
PutOperatorsImplementation() - It's here that DataContext.SubmitChanges() and TransactionScope.Complete() is called. Should I write tests to test what is written to the database? And then what? Delete them the records? Not sure what to do here.
I think I should delete the tests for:
PutOperators() - Auto generated code, one line, calls PutOperatorsImplementation()
PutEntities()- Just a for loop calling PutEntity(), and it's a generic method on a base class
PutEntity() - Calls three methods that already have unit tests and the calls DataContext.InsertOnSubmit.
I have a similar path for getting the data as well:
GetOperatorsResponse GetOperators(GetOperatorsRequest request) - Auto generated
GetOperatorsResponse GetOperatorsImplementation(GetOperatorsRequest input) - Set up DataContext
List<Operator> GetEntities() - Linq Query
Operator ToOperator(User) - Flattens one data entity into it's equivalent BL entity.
I think I should just be testing ToOperator() and GetEntities()
Should I just have a dedicated test database with known good test data in it?
Is that the correct way to approach this?
There is no "hard and fast" rule as to what you should and should not test.
Unit tests are there to Test any implementation your'e writing works and to enable you to be confident when re-factoring that you haven't broke anything. You need to consider whether the tests you write will give you value or not.
The main things you need to consider when deciding what code to cover are
How likely is the code I'm about to write / have written likely to
change and need refactoring?
Is the code written mainly custom code or autogenrated - If
autogenrated then there is little value in writing tests as your
simply just testing the autogenerator you are using does its job
properly (and you should be able to trust it).
You should not use databases or anything that can change outside of the test environment to test Data access code. Instead consider writing "Mocks" to mock the response from the data layer for your tests. This will ensure your tests are consistent. Consider looking at some mocking frameworks such as Rhino Mocks Or MOQ.
Always remember you are writing test for a reason and not for the sake of writing test. If you are not going to gain any value from the tests you write(e.g. if your codebase is not going to change) then simply don't write them.

What is the correct way to unit test a sequence of complex objects

What is the correct way to unit test a method that returns a sequence of complex objects?
I'm implementing a branch finder. I have a repository that returns a Branch object which has various information such as
Branch name
Address
Location
Products stocked
I have a repository that I've faked to always return a known sequence of Branch objects and I need to test the logic in my BranchFinder service.
The Find method takes a location (lat/long) and a product specification in the form of a Flags enum .
public IEnumerable<Branch> Find(Location location, Products products);
Knowing the data that the search is going to operate on, I can safely specify a location and product specification and know what to expect, but how do I assert that the sequence is correct.
e.g.
var loc = new Location(53.79424, -1.546112);
var results = service.Find(loc, Products.Milk | Products.Bananas);
I know for this given query that a certain subset of data should be returned in a certain order, and also that the distance from the current location to the branch should increase as you iterate through the result set.
What is the correct/best way to test these rules and situations?
First thing I would do would be to make sure you access the Service via an interface ILocatorService. Also make sure that any downstream dependency it has to get the Branch info also used via Interface (so that you can inject Mock later on in your test).
Also, be clear that you are testing ILocatorService itself (to make sure that any change you made in the service doest break it).
For Any Dependencies that ILocatorService uses, you can use RhynoMocks to return set of results (it might be going to DB or some other vendor app to give us list of branches)
That way your inputs to ILocatorService stays the same and the only thing you are testing is if any logic that you have changed is breaking existign test/functionality.
If you know how many results are retrieved from the Find method you could convert to a list or array and check each position to check if the Branch object has the correct values, that's how I'd do it.

Categories