Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 1.27 KB

ex6-3.md

File metadata and controls

40 lines (29 loc) · 1.27 KB

6.3 Running Unit Tests in Your Dart Application

Example Code:

The code provided defines a test for the Travel class. It uses the expect() function from the test package to assert that the expected value is equal to the actual value. The test ensures that the distance property is set correctly when the Travel class is instantiated.

The test first creates two variables:

  • distance
  • expectedDistance.

The distance variable is assigned the value 10.0. The expectedDistance variable is assigned the same value.

The test then creates an instance of the Travel class and passes the value of expectedDistance to the constructor. The test then creates a variable called result and sets it to the value of the distance property on the Travel instance.

The test then asserts that the value of result is equal to the value of expectedDistance. This ensures that the distance property is set correctly when the Travel class is instantiated.

import 'package:test_dart_sample/test_dart_sample.dart';

void main() {
  test('Travel Distance', () {
    // Arrange
    var distance = 10.0;
    var expectedDistance = distance;
    
    // Act
    var travel = Travel(expectedDistance);
    var result = travel.distance;
    
    // Assert
    expect(expectedDistance, result);
  });
}