Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 12 Testing

var time = new DateTime(2022, 01, 01);

var clockWidget = new ClockWidgetView();

Assert.Null(clockWidget.Text);

clockWidget.WidgetViewModel = new MockClockWidgetView Model(time);

Assert.Equal(time.ToString(), clockWidget.Text);

}

}

The test TextIsUpdatedByTimeProperty creates a new ClockWidgetView, assigns a your new MockClockWidgetViewModel, and then verifies that that the Text property of the widget is correctly updated to reflect the value from the Time property on your view model through its binding.

Device Testing

Device testing is really a form of unit testing; however, it provides some unique abilities so it deserves its own top-level section. It essentially enables you to write unit tests that can be run on a device and therefore truly test any platform-specific pieces of functionality. A perfect example of this is to test the PlatformLocationService you implemented in the previous chapter to return the longitude and latitude coordinates of each platform provider’s headquarters.

382

Chapter 12 Testing

Creating a Device Test Project

You need to create another project in order to handle the running of the device tests. The documentation on the GitHub repository covers all that is needed, so go to https://github.com/shinyorg/xunit-maui. Check in the code repository called WidgetBoard.DeviceTests if you get stuck; there is an already created project to use as a template.

Adding a Device-Specific Test

using WidgetBoard.Services; using Xunit;

namespace WidgetBoard.DeviceTests.Services;

public class PlatformLocationServiceTests

{

[Fact]

public async Task GetLocationAsyncWillReturnPlatform SpecificLocation()

{

var locationService = new PlatformLocationService();

var location = await locationService. GetLocationAsync();

#if ANDROID

Assert.Equal(37.419857, location.Latitude); Assert.Equal(-122.078827, location.Longitude);

#elif WINDOWS

Assert.Equal(47.639722, location.Latitude); Assert.Equal(-122.128333, location.Longitude);

#else

Assert.Equal(37.334722, location.Latitude);

383

Chapter 12 Testing

Assert.Equal(-122.008889, location.Longitude);

#endif

}

}

Now that you have written your tests, you can run them on your devices.

Running Device-Specific Tests

In order to run your tests on a device, you first need to set your WidgetBoard.DeviceTests project as the startup project. You can do this as follows:

•\

Right-click the WidgetBoard.DeviceTests project in

 

Solution Explorer.

•\

Select Set as Startup Project.

Now start the application from Visual Studio., Figure 12-3 shows the device test runner screen running on Windows.

384

Chapter 12 Testing

Figure 12-3.  Device test runner on the Windows platform

You can click on a specific test and choose to run it, or you can simply Run All Tests. This part is entirely manual so it will require a human to perform these tasks but it can be left to run for as long as the tests need.

Finally you will see the results of the test runs and you can click them to see more information. Figure 12-4 shows the device test runner and a set of test results.

385