iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😺

Applying Property-Based Testing to an Electric Kettle (4): Refactoring Boiling and Warm State Transitions

に公開

Introduction

Following the previous article, Trying Property-based testing on a hot topic pot (3) Boiling/Keep Warm - Thinking about state transitions, I have been exploring Property-based testing using fast-check.
As the subject matter, I decided to use the "Hot Topic Pot Requirement Specifications (Model GOMA-1015) 7th Edition", which is specified as the test base for the Test Design Contest U-30 Class.
It seems that fast-check also has an implementation for finding failing test cases by random-walking through states. I've been thinking about trying out this feature while considering state transitions, but...
Today's post won't be about property-based testing itself, but rather focused on implementation fixes.

Preparation

I have placed the code implemented this time here.
If you have a Node.js environment at hand, you can try it out.

git clone https://github.com/freddiefujiwara/goma-1015.git
cd goma-1015
npm i
npm test

Replace status indicators like _isOpen, connected, etc. with enum State

As mentioned before, states will become more complex in the future, so let's manage them properly using State.
Currently, there are four states: off, open/off, idle, and open/on.
Since boiling and keep-warm states will be needed later, let's define them in advance.

image.png

export enum State {
  OFF = -1,
  OFF_OPEN,
  ON_IDLE,
  ON_OPEN,
  ON_ACTIVE_BOIL,
  ON_ACTIVE_KEEP,
}

I will rewrite the parts that were previously distinguished by isOpen or this._on to look at the State instead.
The diff looks something like this:
image.png
It looks like a lot has changed, but basically, I'm just properly identifying state transitions with switch statements and transitioning to the appropriate state.

Try passing the tests

Let's work hard on debugging until the tests pass.
image.png

I basically only changed the implementation and barely modified the test code. It's truly convenient to have test code when doing this kind of refactoring, isn't it?

Finally

Next time, I'd like to finally realize model-based-testing.
Also, if there are any mistakes in the code, Pull requests are very welcome.
It's been a bit long, but thank you very much for reading to the end.

Discussion