Unittesting and mocking?

johsv448johsv448 Member Posts: 2
edited 2020-10-23 in NAV Three Tier
Hi everyone this is my first post here so please be gentle.

I was recently granted the honor of my company to start getting to know the inner workings of NAV and now BC14 on-prem in order to be a better orderer for our dynamics partner. Being a C#-developer I soon became interested in getting to know how to code in C/AL and how to make configurations with out having to involve hour partner in every little small change we need to make.

We now have a need to calculate the price for our orders without actually creating a sales order. The creation of sales orders are handled by our accounting department at a later time, we would like to get an estimate price before that is done. In order to not create duplicate code that calculates the cost for our order and make maintaining this logic more of a nightmare than it already is, I would like to refactor the code into separate functions that can be called separately.
The current flow looks something like "Clickbutton to create sales order" -> CreateFilterForReport -> validate selection(ReportObject) -> more validation(CodeUnit) -> split order into calculatable parts(Same CodeUnit) -> calculate price and create salesorder intertwined(Same CodeUnit). And every step invokes the next, it's not like the are invoked one at a time. No the steps call upon each other to achive this.

I have no idea if it is considered good practice to build flows like this but I do know that this is fairly business critical, so I am not too keen on going to town tweaking this. Of course there are no unittests what so ever, so that is were I would like to start.

So to get to my question: Is it possible to mock calls to tables such as sales header and sales order line, so I can start refactoring this mess and still be certain things are executed in the way it is supposed to?
From what I have heard it is possible to set a variable in C/AL to be temporary and that the records written during the execution to be stored only in tempdb, but wouldn't that require me to somehow change the codeunits variable setting from non-temp to temp during the run of the unittest?

Answers

  • johsv448johsv448 Member Posts: 2
    I just realized I posted this in the wrong forum as this should belong in NAV Three tier, not Classic. Sorry about that. Could any moderator please move this post?
  • Wisa123Wisa123 Member Posts: 308
    The current flow looks something like "Clickbutton to create sales order" -> CreateFilterForReport -> validate selection(ReportObject) -> more validation(CodeUnit) -> split order into calculatable parts(Same CodeUnit) -> calculate price and create salesorder intertwined(Same CodeUnit).
    sounds like you messed with orders quite a bit :smiley:
    So to get to my question: Is it possible to mock calls to tables such as sales header and sales order line, so I can start refactoring this mess and still be certain things are executed in the way it is supposed to?
    If you want to compare to real-live data it would be easiest to copy the DB (since youre OnPrem) and work on that copy, this will also assure you don't destroy anything.

    If you want to get into Unit-Testing in BC/AL I highly recommend Luc's book: amazon.com
    From what I have heard it is possible to set a variable in C/AL to be temporary and that the records written during the execution to be stored only in tempdb, but wouldn't that require me to somehow change the codeunits variable setting from non-temp to temp during the run of the unittest?

    You can use temporary Records(!) to buffer Data In-Memory. We use that to create database-records for in-between steps that we don't want commited to the database (since we are not object oriented and can make stuff up on the go :wink: ). Those just exist in the Server Memory and will not be commited to tempdb.

    Austrian NAV/BC Dev
  • krikikriki Member, Moderator Posts: 9,112
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • markclemarkcle Member Posts: 65
    johsv448 wrote: »
    I just realized I posted this in the wrong forum as this should belong in NAV Three tier, not Classic. Sorry about that. Could any moderator please move this post?

    Did you ever figure this out? I would love to be able use mocks
Sign In or Register to comment.