I try to use BUnit for component testing in a Blazor server-side project. I am quite new to this and tried to start a real simple setup:
using var ctx = new TestContext();
ctx.Services.AddTransient<IProposalService, ProposalService>();
var cut = ctx.Render<Proposals>();
Now I can't run this since Rider states:
Method 'Render' has 1 parameter(s) but is invoked with 0 argument(s)
After this, I checked in the documentation how to pass arguments for this.
Since my component Proposals does not have any [parameter] tag in it only two injections:
[Inject] private IProposalService ProposalService { get; set; }
[Inject] private NavigationManager NavigationManager { get; set; }
So I though there might be some ChildContent missing, since I am using a ProposalCard like so:
@foreach (var prop in _proposalList)
{
<ProposalCard Proposal="@prop"/>
}
referring to the section Rendering a component under test inside other components in the last section from the documentation link above.
Here they use the following code:
public class NestedComponentTest
{
[Fact]
public void Test()
{
using var ctx = new TestContext();
var wrapper = ctx.RenderComponent<Wrapper>(parameters => parameters
.AddChildContent<HelloWorld>()
);
var cut = wrapper.FindComponent<HelloWorld>();
}
}
I tried like so:
var cut = ctx.Render<Proposals>(parameters => parameters.AddChildContent<ProposalCard>());
- the first render: Method 'Render' has 1 parameter (s) but iS invoked with 0 argument (s)
- the second render: Cannot resolve symbol 'AddChildContent'
- when I run the first render: ProposalTest.cs(35, 35): [CS7036] There is no argument given that corresponds to the required formal parameter 'renderFragment' of 'TestContext. Render (RenderFragment)'
- when running the second render: ProposalTest.cs (37, 35): [CS1061] 'RenderTreeBuilder' does not contain a definition for 'AddChildContent' and no accessible extension method 'AddChildContent' accepting a first argument of type 'RenderTreeBuilder' could be found (are you missing a using directive or an assembly reference?)
I thought maybe I have to create the renderFragment myself with new renderFragment(..) and pass it though I feel really puzzled with this right now and was not able to find any reference for this within the documentation which make it seem kind of a weird idea.
How can I make the render method work for my context here?
update
it seems a little bit better but still now working
Though when I read the second exmpample from here I seems like I shouldn't be passing anything at all, since the are able to just call
var cut = RenderComponent<FetchData>();