{"id":69623,"date":"2022-04-26T08:00:56","date_gmt":"2022-04-26T06:00:56","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=69623"},"modified":"2022-04-26T08:18:24","modified_gmt":"2022-04-26T06:18:24","slug":"test-blazor-webassembly-bunit","status":"publish","type":"post","link":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/","title":{"rendered":"Introduction to Testing Blazor WebAssembly With bUnit"},"content":{"rendered":"<p>In this article, we are going to learn about unit testing a Blazor WebAssembly Project using the bUnit testing library.<\/p>\n<p>If you find some of the concepts we are testing hard to understand, we have a great <a href=\"https:\/\/code-maze.com\/blazor-webassembly-series\/\" target=\"_blank\" rel=\"noopener\">Blazor WebAssembly Series<\/a> that is worth checking out.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/blazor-libraries\/BlazorbUnit\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>With that, let&#8217;s start.<\/p>\n<h2>How to Test Blazor WebAssembly?<\/h2>\n<p><strong>To test a Blazor component, we need to render the component with any relevant input required so we can inspect the rendered markup<\/strong>. Also, we want to trigger event handlers and life cycle methods, to allow us to assert that the component is behaving as we expect it to. All of this is available to us through the <a href=\"https:\/\/bunit.dev\/index.html\" target=\"_blank\" rel=\"nofollow noopener\">bUnit testing library<\/a>.<\/p>\n<p>It is important to note that <strong>dependencies such as CSS are not rendered<\/strong> <strong>when running unit tests<\/strong>. In order to verify that our component is styled correctly, this would require E2E (End-to-End) or Snapshot testing, which would render the component in a browser, along with any styling we have applied. We will only cover unit testing in this article.<\/p>\n<p>As testing Blazor components is very similar to testing normal C# code, we can use testing frameworks that we are familiar with, such as <em>xUnit<\/em>, <em>NUnit<\/em>, or <em>MSTest<\/em>. <strong>We use these testing frameworks in conjunction with bUnit to allow us to write stable unit tests for our Blazor components<\/strong>.<\/p>\n<h2>How To Write bUnit Tests?<\/h2>\n<p>There are some slight differences in writing tests for our Blazor components compared to normal C# code. <strong>bUnit renders the components<\/strong> <strong>through the <\/strong><code>TestContext<\/code> <strong>class,<\/strong> which provides us access to the rendered component instance, so that we can interact with the component.<\/p>\n<p>We can write our unit tests in either <code>.cs<\/code> or <code>.razor<\/code> files. Writing tests in <code>.razor<\/code> files provide an easier way to declare components and HTML markup, without having to do any character escaping. However, we need to ensure that the test project is set to the <code>Microsoft.NET.Sdk.Razor<\/code> SDK type, otherwise the <code>.razor<\/code> files won&#8217;t compile. As more people are probably familiar with writing tests in <code>.cs<\/code> files, we are going to use this for the demo.<\/p>\n<h2>Create Blazor WebAssembly Project<\/h2>\n<p>Firstly, we create a Blazor WebAssembly project by using the template from Visual Studio or the <code>dotnet new blazorwasm<\/code> command.\u00a0<\/p>\n<p>Next, let&#8217;s create our component in the Pages folder that we&#8217;ll use for testing:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;p&gt;Hello from TestComponent&lt;\/p&gt;\r\n\r\n@code {\r\n}<\/pre>\n<p>We create a very simple component with a <code>p<\/code> element and some text, which is enough for us to write our first test.<\/p>\n<h2>Create xUnit Test Project<\/h2>\n<p>With our basic component created, let&#8217;s create a test project, by using the xUnit template provided by Visual Studio or the <code>dotnet new xunit<\/code> command.\u00a0<\/p>\n<p>We need to remember to add the <code>bunit<\/code> NuGet package to this project, along with a reference to our Blazor WebAssembly project. Let&#8217;s write our first test to ensure the markup of our component matches what we expect.<\/p>\n<h2>Test Markup Matching<\/h2>\n<p>We&#8217;ll start by creating a class where we are going to write our tests:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class TestComponentTests : TestContext\r\n{\r\n}<\/pre>\n<p>We inherit the <code>TestContext<\/code> class, which saves us from having to create a new one for each test to render our component.<\/p>\n<p>With this, we are ready to write our first test:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponent()\r\n{\r\n    var cut = RenderComponent&lt;TestComponent&gt;();\r\n\r\n    cut.MarkupMatches(\"&lt;p&gt;Hello from TestComponent&lt;\/p&gt;\");\r\n}<\/pre>\n<p>First, we decorate the method with the xUnit <code>Fact<\/code> attribute, to let the test runner know this is a test method. Next, we render our <code>TestComponent<\/code>. Finally, we verify that the markup matches the expected string.<\/p>\n<h2>Add Parameters to Component<\/h2>\n<p>More often than not, our components require input, in the form of <a href=\"https:\/\/code-maze.com\/blazor-components\/\" target=\"_blank\" rel=\"noopener\">parameters<\/a>, so let&#8217;s create a component with one:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">&lt;p&gt;Message: @Message&lt;\/p&gt;\r\n\r\n@code {\r\n    [Parameter]\r\n    public string Message { get; set; }\r\n}<\/pre>\n<p>Now we have a <code>Message<\/code> parameter that we simply render in a <code>p<\/code> element.<\/p>\n<p>Next, we&#8217;ll create a new test:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponentWithParameter()\r\n{\r\n    var message = \"Message from test\";\r\n\r\n    var cut = RenderComponent&lt;TestComponentWithParameter&gt;(parameters =&gt; parameters.Add(p =&gt; p.Message, message));\r\n\r\n    cut.MarkupMatches($\"&lt;p&gt;Message: {message}&lt;\/p&gt;\");\r\n}<\/pre>\n<p>bUnit provides us with a strongly typed builder that we can use to pass parameters to our component. We use the\u00a0<code>Add()<\/code> method from the <code>ComponentParameterCollectionBuilder<\/code> class to select our <code>Message<\/code> parameter and pass in the <code>message<\/code> we defined in our test method. Again, we test that the markup matches, ensuring to include our new message text.<\/p>\n<h2>Trigger Event Handlers<\/h2>\n<p><strong>Blazor allows us to bind to element event handlers<\/strong>, which we can also create tests for, so let&#8217;s create a new component that includes an event handler:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">&lt;p&gt;Button clicked: @buttonClicked&lt;\/p&gt;\r\n\r\n&lt;button @onclick=\"OnClick\"&gt;Click me&lt;\/button&gt;\r\n\r\n@code {\r\n    bool buttonClicked = false;\r\n    \r\n    void OnClick() =&gt; buttonClicked = true;\r\n}<\/pre>\n<p>We create a simple <code>onclick<\/code> event handler for the <code>button<\/code>, that sets <code>buttonClicked<\/code> to <code>true<\/code>.<\/p>\n<p>Next, we can write a new test method to test our event handler:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponentWithEventHandler()\r\n{\r\n    var cut = RenderComponent&lt;TestComponentWithEventHandler&gt;();\r\n\r\n    cut.Find(\"button\").Click();\r\n\r\n    cut.Find(\"p\").MarkupMatches(\"&lt;p&gt;Button clicked: True&lt;\/p&gt;\");\r\n}<\/pre>\n<p>First, we render our new component. Then, we use the <code>IRenderedComponent<\/code> to find our <code>button<\/code> element, which returns an <a href=\"https:\/\/github.com\/AngleSharp\/AngleSharp\" target=\"_blank\" rel=\"nofollow noopener\">Anglesharp<\/a> Dom element. This allows us to call the <code>Click()<\/code> method, which executes our <code>onclick<\/code> event handler. Finally, we find our <code>p<\/code> element and verify the markup matches.<\/p>\n<p>If our event handler makes use of the <code>EventArgs<\/code> parameter, we can optionally provide this information in our test.<\/p>\n<p>Let&#8217;s refactor our component to use one of the <code>MouseEventArgs<\/code> properties:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">&lt;p&gt;Control key pressed: @controlKeyPressed&lt;\/p&gt;\r\n\r\n&lt;button @onclick=\"OnClick\"&gt;Click me&lt;\/button&gt;\r\n\r\n@code {\r\n\r\n    bool controlKeyPressed = false;\r\n\r\n    void OnClick(MouseEventArgs e) =&gt; controlKeyPressed = e.CtrlKey;\r\n}<\/pre>\n<p>This time, instead of unconditionally setting our variable to <code>true<\/code>, we bind it to the <code>CtrlKey<\/code> property, which is set to <code>true<\/code> if the Ctrl key is pressed when clicking the button.<\/p>\n<p>We must also change our test to account for this new functionality:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponentWithEventHandler()\r\n{\r\n    var cut = RenderComponent&lt;TestComponentWithEventHandler&gt;();\r\n\r\n    cut.Find(\"button\").Click(ctrlKey: true);\r\n\r\n    cut.Find(\"p\").MarkupMatches(\"&lt;p&gt;Control key pressed: True&lt;\/p&gt;\");\r\n}<\/pre>\n<p>This time, we pass the <code>ctrlKey<\/code> optional parameter to the <code>Click()<\/code> method. We must also remember to update the <code>MarkupMatches<\/code> parameter as our markup has changed.<\/p>\n<h2>Service Injection<\/h2>\n<p><strong>It is good practice to keep our components free from complex logic by using services and injecting them into the components that require them<\/strong>. bUnit provides us with the <code>Services<\/code> collection, so we can register any dependencies our component might need when writing our tests.<\/p>\n<p>Let&#8217;s create a simple service interface to return some data:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public interface IDataService()\r\n{\r\n    List&lt;string&gt; GetData();\r\n}<\/pre>\n<p>And a class that implements this interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class DataService : IDataService\r\n{\r\n    public List&lt;string&gt; GetData() =&gt; new List&lt;string&gt; { \"Data 1\", \"Data 2\" };\r\n}<\/pre>\n<p>With this service in place, let&#8217;s create a component that uses it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">@inject IDataService DataService;\r\n\r\n@if (MyData is null)\r\n{\r\n    &lt;p&gt;Retrieving data...&lt;\/p&gt;\r\n}\r\nelse\r\n{\r\n    &lt;p&gt;Data retrieved&lt;\/p&gt;\r\n}\r\n\r\n@code {\r\n\r\n    public List&lt;string&gt; MyData { get; set; }\r\n\r\n    protected override void OnInitialized()\r\n    {\r\n        MyData = DataService.GetData();\r\n    }\r\n}\r\n<\/pre>\n<p>First, we inject <code>IDataService<\/code>, and conditionally render a <code>p<\/code> element depending on whether <code>MyData<\/code> is null. In the <code>OnInitialized<\/code> life cycle method, we retrieve the data from our service.<\/p>\n<p>Now we can write a test to verify <code>MyData<\/code> is populated correctly:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponentWithInjection()\r\n{\r\n    Services.AddSingleton&lt;IDataService, DataService&gt;();\r\n\r\n    var cut = RenderComponent&lt;TestComponentWithInjection&gt;();\r\n\r\n    Assert.NotNull(cut.Instance.MyData);\r\n}<\/pre>\n<p>The first thing we must do is register our service, using the <code>Services<\/code> collection. Next, we render our component, and finally, we assert that <code>MyData<\/code> is not null, by accessing the component under test <code>Instance<\/code> property.<\/p>\n<h2>Test JSInterop<\/h2>\n<p>When we build applications with Blazor, we often need to interact with JavaScript using the <a href=\"https:\/\/code-maze.com\/how-to-call-javascript-code-from-net-blazor-webassembly\/\" target=\"_blank\" rel=\"noopener\">JSInterop<\/a>. With bUnit, functionality is provided to emulate the <code>IJSRuntime<\/code> that we inject into our components to interact with JavaScript.<\/p>\n<p>Let&#8217;s start by creating a component that invokes some JavaScript:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">@inject IJSRuntime JSRuntime\r\n\r\n&lt;button @onclick=\"ShowAlert\"&gt;Show Alert&lt;\/button&gt;\r\n\r\n@code {\r\n    private async Task ShowAlert() =&gt; await JSRuntime.InvokeVoidAsync(\"alert\", \"Alert from Blazor component\");\r\n}<\/pre>\n<p>First, we inject the <code>IJSRuntime<\/code>. Then we create a button that has an <code>onclick<\/code> event handler that calls the <code>ShowAlert<\/code> method. In this method, we invoke the JavaScript <code>alert<\/code> method, passing in a message to display.<\/p>\n<p>Now we can write a test for this component:\u00a0<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponentWithJSInterop()\r\n{\r\n    JSInterop.SetupVoid(\"alert\", \"Alert from Blazor component\");\r\n\r\n    var cut = RenderComponent&lt;TestComponentWithJSInterop&gt;();\r\n\r\n    cut.Find(\"button\").Click();\r\n    \r\n    JSInterop.VerifyInvoke(\"alert\", calledTimes: 1);\r\n}<\/pre>\n<p>First, we need to set up the bUnit <code>JSInterop<\/code>, by calling the <code>SetupVoid()<\/code> method, passing in our <code>alert<\/code> method and message we want to show. Next, we render our component, and then find our button and call the <code>Click()<\/code> method. Finally, we verify our <code>alert<\/code> method was called once through the <code>JSInterop<\/code>.<\/p>\n<h2>Mock HttpClient<\/h2>\n<p>As Blazor WebAssembly is a purely client-side framework, we often need to interact with a server-side API over HTTP, which we do by using the <a href=\"https:\/\/code-maze.com\/blazor-webassembly-httpclient\/\" target=\"_blank\" rel=\"noopener\">HttpClient<\/a>.<\/p>\n<p>Let&#8217;s create a component and inject <code>HttpClient<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">@inject HttpClient Http;\r\n\r\n@if (DataFromApi is null)\r\n{\r\n    &lt;p&gt;Retrieving data from API...&lt;\/p&gt;\r\n}\r\nelse\r\n{\r\n    &lt;p&gt;Data from API retrieved&lt;\/p&gt;\r\n}\r\n\r\n@code {\r\n    public List&lt;string&gt; DataFromApi { get; set; }\r\n\r\n    protected override async Task OnInitializedAsync()\r\n    {\r\n  \tDataFromApi = await Http.GetFromJsonAsync&lt;List&lt;string&gt;&gt;(\"\/api\/data\");\r\n    }\r\n}<\/pre>\n<p>First of all, we inject our <code>HttpClient<\/code>. Similar to our component that injects a service, we conditionally render a <code>p<\/code> element depending on whether <code>DataFromApi<\/code> is null or not. In the <code>OnInitializedAsync<\/code> method, we retrieve our data from the <code>\/api\/data<\/code> endpoint.<\/p>\n<p>Now we can write a unit test for this component. As we are injecting an <code>HttpClient<\/code>, we need to mock it, which we can do using the <a href=\"https:\/\/github.com\/richardszalay\/mockhttp\" target=\"_blank\" rel=\"nofollow noopener\">RichardSzalay.MockHttp<\/a> package. This is not the only way to achieve this as we could create our own mock of <code>HttpClient<\/code> using a library such as <a href=\"https:\/\/github.com\/moq\/moq4\" target=\"_blank\" rel=\"nofollow noopener\">Moq<\/a>. <strong>However, we will only focus on the RichardSzalay package for this article<\/strong>.<\/p>\n<p>With this package installed, let&#8217;s write our test:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponentWithHttpClient()\r\n{\r\n    var content = JsonSerializer.Serialize(new List&lt;string&gt; { \"data\" });\r\n    \r\n    var mockHttp = new MockHttpMessageHandler();\r\n    var httpClient = mockHttp.ToHttpClient();\r\n    httpClient.BaseAddress = new Uri(\"http:\/\/localhost\");\r\n\r\n    Services.AddSingleton(httpClient);\r\n\r\n    mockHttp.When(\"\/api\/data\")\r\n            .Respond(HttpStatusCode.OK, \"application\/json\", content);\r\n\r\n    var cut = RenderComponent&lt;TestComponentWithHttpClient&gt;();\r\n\r\n    cut.WaitForAssertion(() =&gt; Assert.NotNull(cut.Instance.DataFromApi));\r\n}<\/pre>\n<p>The first thing we do is create some JSON data that is going to be returned from our mocked <code>HttpClient<\/code>. Next, we create a new <code>MockHttpMessageHandler<\/code> and then call the <code>ToHttpClient<\/code> method to get our <code>HttpClient<\/code>, also making sure to set the <code>BaseAddress<\/code>. Then, we register the <code>HttpClient<\/code> with the <code>Services<\/code> collection.\u00a0<\/p>\n<p>Now we need to set up the <code>MockHttpMessageHandler<\/code> to respond to requests to <code>\/api\/data<\/code> and return our JSON data. As usual, we register our component, and finally, as we are using an asynchronous method to retrieve our data, we need to use <code>WaitForAssertion()<\/code>, checking that our <code>DataFromApi<\/code> list is not null.<\/p>\n<h2>Mock NavigationManager with bUnit<\/h2>\n<p>Blazor provides a <code>NavigationManager<\/code> service, which can be injected into our component to give us browser navigation. bUnit provides a fake version of <code>NavigationManager<\/code> which is added by default to the <code>TestContext.Services<\/code> collection.<\/p>\n<p>Let&#8217;s create a component that uses the <code>NavigationManager<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">@inject NavigationManager NavigationManager\r\n\r\n&lt;button @onclick=\"NavigateToHome\"&gt;Navigate to Home&lt;\/button&gt;\r\n\r\n@code {\r\n    void NavigateToHome() =&gt; NavigationManager.NavigateTo(\"\/home\");\r\n}<\/pre>\n<p>First, we inject the <code>NavigationManager<\/code> class and add a button with an <code>onclick<\/code> handler. In our <code>NavigateToHome<\/code> method, we simply call <code>NavigateTo<\/code> and navigate to <code>\/home<\/code>.<\/p>\n<p>Now we can create a test to ensure our component correctly invokes the <code>NavigationManager<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void TestComponentWithNavigationManager()\r\n{\r\n    var navigationManager = Services.GetRequiredService&lt;FakeNavigationManager&gt;();\r\n    var cut = RenderComponent&lt;TestComponentWithNavigationManager&gt;();\r\n\r\n    cut.Find(\"button\").Click();\r\n\r\n    Assert.Equal($\"{navigationManager.BaseUri}home\", navigationManager.Uri);\r\n}<\/pre>\n<p>Firstly, we want to get the <code>FakeNavigationManager<\/code> provided by bUnit. Next, we render our component and find our button so we can execute the <code>Click<\/code> event. To test that our navigation worked correctly, we can compare our expected Uri of <code>http:\/\/localhost\/home<\/code> to the <code>NavigationManager.Uri<\/code> property.<\/p>\n<h2>Conclusion<\/h2>\n<p>Now we have a good understanding of how to write unit tests for our Blazor components with bUnit. Unit testing gives us the confidence that our code does what we expect it to, and allows us to safely refactor code, knowing that we haven&#8217;t broken any piece in the process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to learn about unit testing a Blazor WebAssembly Project using the bUnit testing library. If you find some of the concepts we are testing hard to understand, we have a great Blazor WebAssembly Series that is worth checking out. With that, let&#8217;s start. How to Test Blazor WebAssembly? To [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62188,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[684,12],"tags":[685,686,1224,576,173],"class_list":["post-69623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blazor-webassembly","category-csharp","tag-blazor","tag-blazor-webassembly","tag-bunit","tag-testing","tag-unit-testing","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Introduction to Testing Blazor WebAssembly With bUnit - Code Maze<\/title>\n<meta name=\"description\" content=\"bUnit is one of the tools\/libraries we can use to test a Blazor WebAssembly Project and make sure it works as expected.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to Testing Blazor WebAssembly With bUnit - Code Maze\" \/>\n<meta property=\"og:description\" content=\"bUnit is one of the tools\/libraries we can use to test a Blazor WebAssembly Project and make sure it works as expected.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-26T06:00:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-26T06:18:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Introduction to Testing Blazor WebAssembly With bUnit\",\"datePublished\":\"2022-04-26T06:00:56+00:00\",\"dateModified\":\"2022-04-26T06:18:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/\"},\"wordCount\":1544,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png\",\"keywords\":[\"Blazor\",\"Blazor WebAssembly\",\"bUnit\",\"Testing\",\"Unit Testing\"],\"articleSection\":[\"Blazor WebAssembly\",\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/\",\"url\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/\",\"name\":\"Introduction to Testing Blazor WebAssembly With bUnit - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png\",\"datePublished\":\"2022-04-26T06:00:56+00:00\",\"dateModified\":\"2022-04-26T06:18:24+00:00\",\"description\":\"bUnit is one of the tools\/libraries we can use to test a Blazor WebAssembly Project and make sure it works as expected.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png\",\"width\":1100,\"height\":620,\"caption\":\"Blazor WebAssembly\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction to Testing Blazor WebAssembly With bUnit\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to Testing Blazor WebAssembly With bUnit - Code Maze","description":"bUnit is one of the tools\/libraries we can use to test a Blazor WebAssembly Project and make sure it works as expected.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to Testing Blazor WebAssembly With bUnit - Code Maze","og_description":"bUnit is one of the tools\/libraries we can use to test a Blazor WebAssembly Project and make sure it works as expected.","og_url":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/","og_site_name":"Code Maze","article_published_time":"2022-04-26T06:00:56+00:00","article_modified_time":"2022-04-26T06:18:24+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Introduction to Testing Blazor WebAssembly With bUnit","datePublished":"2022-04-26T06:00:56+00:00","dateModified":"2022-04-26T06:18:24+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/"},"wordCount":1544,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png","keywords":["Blazor","Blazor WebAssembly","bUnit","Testing","Unit Testing"],"articleSection":["Blazor WebAssembly","C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/","url":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/","name":"Introduction to Testing Blazor WebAssembly With bUnit - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png","datePublished":"2022-04-26T06:00:56+00:00","dateModified":"2022-04-26T06:18:24+00:00","description":"bUnit is one of the tools\/libraries we can use to test a Blazor WebAssembly Project and make sure it works as expected.","breadcrumb":{"@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-blazor-wasm.png","width":1100,"height":620,"caption":"Blazor WebAssembly"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/test-blazor-webassembly-bunit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Introduction to Testing Blazor WebAssembly With bUnit"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/69623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=69623"}],"version-history":[{"count":6,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/69623\/revisions"}],"predecessor-version":[{"id":69638,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/69623\/revisions\/69638"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62188"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=69623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=69623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=69623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}