FsCheck. Difference between the object and the model

164 views Asked by At

I have heard recently about Model Based Testing and searched for tools that can follow this approach.

As the result i found FsCheck.

At the Experimental page, the author describes how to create a model based test which can be executed.

That is all nice and everything, but sadly i do not understand what would be a difference between the actual object and the model of the object.

So, given the following code:

using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleOrderApp
{
    public class Order
    {
        private string _name;
        private string _description;
        private bool _isOnOrderList;

        public Order(string name, string description)
        {
            _name = name;
            _description = description;
            _isOnOrderList = false;
        }

        public string Name { 
            get => _name;
            set 
            {
                if (!_isOnOrderList)
                {
                    return;
                }
                _name = value;
            }
        }

        public string Description
        {
            get => _description;
            set 
            {
                if (!_isOnOrderList)
                {
                    return;
                }

                _description = value;
            }
    }

        public bool IsOnOrderList
        {
            get => _isOnOrderList;
            set => _isOnOrderList = value;
        }
    }
}

Spec: - User is able to provide an order name - User is able to provide an order description - User is not able to update the order if it is in the OrderList (IsOnOrderList = true)


Bug:
The code

if (!_isOnOrderList)
{
    return;
}

Should not have !.


Can somebody help me construct an OrderModel, that i can use to validate my Order object against and explain to me why it has to be done so? Currently, i am very keen to think that Order and OrderModel are identical.

UPD:

Would it be correct to state that the Model has same properties of the Object under test, but the values are simply hardcoded?

1

There are 1 answers

1
Kurt Schelfthout On BEST ANSWER

The model is typically an immutable value, and simpler than the system under test (SUT). Typically for model-based tests the SUT is really a system, i.e. a set of interrelated classes that do something non-trivial, contains multiple data structures etc, or it is a pretty complicated class, e.g. a class that implements a particular data structure, like a B-tree or a concurrent queue. So finding something simpler that does not model exactly everything that goes on in the SUT, or does so in a very naive or ineffecient way, is pretty straightforward.

It's hard to show that kind of example in starter examples because you quickly get lost in the details. For the example you give you could represent your class by a 3-tuple (name, description, isOnOrderList) and go from there. Then operations would be setName, setDescription, setIsOnOrderList. But since your starting example is itself so simple, it's not straightforward to find something even simpler that tests something substantial in this case.

A slightly more elaborate example might help, there is on in the FsCheck source here: https://github.com/fscheck/FsCheck/blob/master/src/FsCheck/Script.fsx#L22 In that code, you can see that the model class is much shorter than the SUT, and is represented by an immutable record type.