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

Chapter 12 Testing

Figure 12-4.  Test run result for the GetLocationAsyncWillReturn PlatformSpecificLocation device test

You can run these tests on all the platforms that you support to make sure that the code does what is expected.

Snapshot Testing

Snapshot testing is similar to unit testing, but it avoids the need to write Assert statements to manually define each expectation in the test. Instead the result of a test is compared to a golden master. A golden master is a snapshot of a previous test run that you as the test author accept as the expected result for subsequent test runs. A snapshot can be anything ranging from a screenshot of the application to a serialization of an object in memory. If you take a look at the WeatherWidgetViewModel you unit tested in the earlier section, you can see that a serialization of the state

of the ValidForecastResultsInSuccessfulLoad test will result in the following golden master being created:

386

Chapter 12 Testing

{

LoadWeatherCommand: {},

IconUrl: https://openweathermap.org/img/wn/abc.png@2x.png, State: Loaded,

Temperature: 18.0,

Weather: Sunshine, Type: Weather

}

When this test is run, each time the serialized output of the WeatherWidgetViewModel will be compared to the above golden master. If any of the values are different from those in the golden master, the test will fail.

Snapshot Testing Your Application

In order to snapshot test your application, you will make use of the excellent library called VerifyTests. VerifyTests has some really great documentation and examples to get you started over at https://github. com/VerifyTests/Verify.

You will additionally need to consume the Verify.Xunit NuGet package. I have opted to create a separate project just to keep things clearly separated for the purpose of this example. You can repeat the steps in sections “Adding a Unit Test Project to Your Solution” and “Adding a Reference to the Project to Test,” except that you will name the project

WidgetBoard.SnapshotTests.

Using VerifyTests, you can take a copy of your

WeatherWidgetViewModelTests class in the WidgetBoard.Tests project and modify it to the following. The limited changes are shown in bold to highlight the differences from the original.

387

Chapter 12 Testing

[UsesVerify]

public class WeatherWidgetViewModelTests

{

[Fact]

public async Task NullLocationResultsInPermissionErrorState()

{

var viewModel = new WeatherWidgetViewModel( new MockWeatherForecastService(null), new MockLocationService(null));

await viewModel.InitializeAsync();

await Verify(viewModel);

}

[Fact]

public async Task NullForecastResultsInErrorState()

{

var viewModel = new WeatherWidgetViewModel( new MockWeatherForecastService(null),

new MockLocationService(new Location(0.0, 0.0)));

await viewModel.InitializeAsync();

await Verify(viewModel);

}

[Fact]

public async Task ValidForecastResultsInSuccessfulLoad()

{

var viewModel = new WeatherWidgetViewModel(

new MockWeatherForecastService(new Communications. Forecast

388

Chapter 12 Testing

{

Current = new Communications.Current

{

Temperature = 18.0,

Weather = new Communications.Weather[]

{

new Communications.Weather

{

Icon = "abc.png",

Main = "Sunshine"

}

}

}

}),

new MockLocationService(new Location(0.0, 0.0)));

await viewModel.InitializeAsync();

await Verify(viewModel);

}

}

You remove the Assert statements and replace them by calling the Verify method. In your original scenario, you were only asserting a small number of things, but you can imagine that if the number of Assert statements were to grow, then this single method call to Verify really does reduce the complexity of your tests.

Brand new tests will always fail until you accept the golden master. There is tooling that can make this task easier, which is again provided by the VerifyTests developers.

389