cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Application Development Tips & Tricks Part 2

No ratings

 

 

Step 3: Test Services and Save Test Cases

 

In the previous step, you created an Entity with a customized Service. You can easily test and update the Service within the same editing window. This allows you to quickly modify a Service and confirm that its new behavior is functioning as expected.

Test Execution

 

To execute a Service, you can do it while still developing and after you have created it.

 

During Development

 

  1. Open the LineCheckSystem Thing.
  2. Select the Services tab.
  3. Click the ListHotLineParts link.

    open_parts_service.png

     

If you scroll to the bottom of the page, you will see the Execute window. Enter a value for the TemperatureThreshold and click Execute.

execute_service_dev.png

 

 

After Development

 

  1. Open the LineCheckSystem Thing.
  2. Select the Services tab.
  3. Click the ListHotLineParts Execute play button.

    execute_service.png

     

  4. Enter a value for the TemperatureThreshold and click Execute.

    execute_service_result.png

     

 

 

Saving Test Cases

 

Store input parameters makes testing during development and after development much faster. Whether you are testing base cases, specific scenarios, or throwing everything you possibly can at your Service, this tab will allow you to store and name test cases for later usage.

During Development

 

  1. Open the LineCheckSystem Thing.
  2. Select the Services tab.
  3. Click the ListHotLineParts link

    open_parts_service (1).png

     

    If you scroll to the bottom of the page, you will see the Execute window. Enter a value for the TemperatureThreshold and click Save Input Set.


    save_service_dev.png



  4. When prompted, enter a name for this test case and click Save Input Set.

 

After Development

 

  1. Open the LineCheckSystem Thing.
  2. Select the Services tab.
  3. Click the ListHotLineParts Execute play button.

    execute_service (1).png

     

  4. Enter a value for the TemperatureThreshold and click Save Input Set.

    save_input_result.png

     

  5. When prompted, enter a name for this test case and click Save Input Set.

 

While this example is pretty simple and straightforward, it is also possible to store other values and other data types! You will be provided with a dropdown of your stored test cases. Just select a case and click Execute.

 
 

Step 4: Utilize Code Auto-Complete Feature

 

  1. If it is not open already, open the ListHotLineParts Service or create a new one.
  2. On a new line, enter ThingTemplates["LinePartTemplate"].
  3. Add a period . after the closing bracket to gain access to the Services and Properties of the Thing Template we created earlier.

    code_completion.png

     

 

You will now see the Services and Properties that were created inside of the LinePartTemplate or passed down in its object-oriented model.

 
 

Step 5: Test Code at Design Time with Linting

 

Linting is a process by which your development environment warns you of possible issues even before any attempt to run or test the code. Instead, an in-editor warning pops up that alerts you to the issue as you’re writing your code. Linting is yet another feature which many IDEs provide outside of the IoT realm.

 

  1. Select the Lint checkbox at the top of the Service-editing section.

     

    lint_scope.png

  2. Type any statement that would cause a Lint warning or error to pop up. In this case, it is best programming practice to use a semi-colons at the end of JavaScript code.

 

Debugging

 

Services within the composer are based on the Rhino JavaScript engine, which is built using Java.

 

It is currently unavailable to see the JavaScript code running within the browser, but you are able to still log what is occurring at each step of development and runtime. ThingWorx provides logging that will go to the ScriptLog under the Monitor section on the left. The logger uses different log levels that will appear differently in the ScriptLog screen.

 

LevelSyntaxExample
infologger.infologger.info("X + 1 = " + 5);
tracelogger.tracelogger.trace("Print this InfoTable - " + table.toJSON());
warninglogger.warnlogger.warn("Print random JSON data - " + JSON.stringify(data));
debuglogger.debuglogger.debug("Adding debug information here.");
errorlogger.errorlogger.error("What kind of error took place? " + err.message);


Testing Tips

 

From the Lint website, here are some common mistakes that JavaScript Lint looks for:

  • Missing semicolons at the end of a line.
  • Curly braces without an if, for, while, etc.
  • Code that is never run because of a return, throw, continue, or break.
  • Case statements in a switch that do not have a break statement.
  • Leading and trailing decimal points on a number.
  • A leading zero that turns a number into octal (base 8).
  • Comments within comments.
  • Ambiguity whether two adjacent lines are part of the same statement.
  • Statements that don't do anything.

JavaScript Lint also looks for the following less common mistakes:

  • Regular expressions that are not preceded by a left parenthesis, assignment, colon, or comma.
  • Statements that are separated by commas instead of semicolons.
  • Use of increment (++) and decrement (--) except for simple statements such as "i++;" or "--i;".
  • Use of the void type.
  • Successive plus (e.g. x+++y) or minus (e.g. x---y) signs.
  • Use of labeled for and while loops.
  • if, for, while, etc. without curly braces. (This check is disabled by default.)

 

 

Step 6: Next Steps 

 

Congratulations! You've successfully completed the Application Development Tools, Tips & Tricks, and learned how to:

 

  • Use Snippets to generate code
  • Execute and test Services
  • Save Service test cases to facilitate QA process
  • Utilize the code auto-completion feature
  • Test code at design time with Lint warnings and errors

 

Learn More

 

We recommend the following resources to continue your learning experience:

 

CapabilityGuide
BuildCreate Custom Business Logic Guide
ExperienceCreate Your Application UI

 

 

Additional Resources

 

If you have questions, issues, or need additional information, refer to:

 

ResourceLink
CommunityDeveloper Community Forum
SupportHelp Center
Version history
Last update:
‎Nov 11, 2022 10:19 AM
Updated by:
Labels (1)
Contributors