|
65 | 65 | <article class="content wrap" id="_content" data-uid="">
|
66 | 66 | <h1 id="options" sourcefile="tutorial/options.md" sourcestartlinenumber="1" sourceendlinenumber="1">Options</h1>
|
67 | 67 |
|
| 68 | +<p sourcefile="tutorial/options.md" sourcestartlinenumber="3" sourceendlinenumber="3">Remember this code?</p> |
| 69 | +<pre sourcefile="tutorial/options.md" sourcestartlinenumber="5" sourceendlinenumber="9"><code class="lang-c#">.WithServices(services => services |
| 70 | + .WithSetupFor<IOptions<AppSettings>>(settings => settings |
| 71 | + .Value.CacheDbResults = false)) |
| 72 | +</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="11" sourceendlinenumber="11">In this section we are going to improve it with the built-in options setup methods.</p> |
| 73 | +<h2 id="options-configuration-setup" sourcefile="tutorial/options.md" sourcestartlinenumber="13" sourceendlinenumber="13">Options configuration setup</h2> |
| 74 | +<p sourcefile="tutorial/options.md" sourcestartlinenumber="15" sourceendlinenumber="15">Go to the <strong>"project.json"</strong> file and add <strong>"MyTested.AspNetCore.Mvc.Options"</strong> as a dependency of the test project:</p> |
| 75 | +<pre sourcefile="tutorial/options.md" sourcestartlinenumber="17" sourceendlinenumber="33"><code class="lang-json">"dependencies": { |
| 76 | + "dotnet-test-xunit": "2.2.0-*", |
| 77 | + "xunit": "2.2.0-*", |
| 78 | + "Moq": "4.6.38-*", |
| 79 | + "MyTested.AspNetCore.Mvc.Authentication": "1.0.0", |
| 80 | + "MyTested.AspNetCore.Mvc.Controllers": "1.0.0", |
| 81 | + "MyTested.AspNetCore.Mvc.DependencyInjection": "1.0.0", |
| 82 | + "MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0", |
| 83 | + "MyTested.AspNetCore.Mvc.Http": "1.0.0", |
| 84 | + "MyTested.AspNetCore.Mvc.ModelState": "1.0.0", |
| 85 | + "MyTested.AspNetCore.Mvc.Models": "1.0.0", |
| 86 | + "MyTested.AspNetCore.Mvc.Options": "1.0.0", // <--- |
| 87 | + "MyTested.AspNetCore.Mvc.ViewActionResults": "1.0.0", |
| 88 | + "MusicStore": "*" |
| 89 | +}, |
| 90 | +</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="35" sourceendlinenumber="35">Go to the unit test asserting the <strong>"Details"</strong> action in the <strong>"StoreManagerControllerTest"</strong> controller and change the following code:</p> |
| 91 | +<pre sourcefile="tutorial/options.md" sourcestartlinenumber="37" sourceendlinenumber="41"><code class="lang-c#">.WithServices(services => services |
| 92 | + .WithSetupFor<IOptions<AppSettings>>(settings => settings |
| 93 | + .Value.CacheDbResults = false)) |
| 94 | +</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="43" sourceendlinenumber="43">With this one:</p> |
| 95 | +<pre sourcefile="tutorial/options.md" sourcestartlinenumber="45" sourceendlinenumber="48"><code class="lang-c#">.WithOptions(options => options |
| 96 | + .For<AppSettings>(settings => settings.CacheDbResults = false)) |
| 97 | +</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="50" sourceendlinenumber="50">Much more readable! :)</p> |
| 98 | +<p sourcefile="tutorial/options.md" sourcestartlinenumber="52" sourceendlinenumber="52">Additionally, the <strong>"TestStartup"</strong> class no longer needs this call:</p> |
| 99 | +<pre sourcefile="tutorial/options.md" sourcestartlinenumber="54" sourceendlinenumber="56"><code class="lang-c#">services.AddScoped<IOptions<AppSettings>, OptionsManager<AppSettings>>(); |
| 100 | +</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="58" sourceendlinenumber="58">Our <strong>"ConfigureTestServices"</strong> should now contain the following:</p> |
| 101 | +<pre sourcefile="tutorial/options.md" sourcestartlinenumber="60" sourceendlinenumber="70"><code class="lang-c#">public void ConfigureTestServices(IServiceCollection services) |
| 102 | +{ |
| 103 | + base.ConfigureServices(services); |
| 104 | + |
| 105 | + services.ReplaceLifetime<IMemoryCache>(ServiceLifetime.Scoped); |
| 106 | + |
| 107 | + services.ReplaceSingleton<SignInManager<ApplicationUser>>(sp => |
| 108 | + MockProvider.SignInManager(sp.GetRequiredService<UserManager<ApplicationUser>>())); |
| 109 | +} |
| 110 | +</code></pre><h2 id="section-summary" sourcefile="tutorial/options.md" sourcestartlinenumber="72" sourceendlinenumber="72">Section summary</h2> |
| 111 | +<p sourcefile="tutorial/options.md" sourcestartlinenumber="74" sourceendlinenumber="74">Well, this was easy. In fact, it it is the easiest part of this tutorial. Let's move to <a href="/tutorial/sessioncache.html" sourcefile="tutorial/options.md" sourcestartlinenumber="74" sourceendlinenumber="74">Session & Cache</a> where we will use the options setup one more time.</p> |
68 | 112 |
|
69 | 113 | </article>
|
70 | 114 | </div>
|
|
0 commit comments