Bunit tests for editForm dependent components #744
-
|
Hello
When I am trying to render my component I have standard error:
error:
And at the moment I don't know how to correctly attach InputDateTime component to the EditForm using Bunit.TestContext or something else. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 7 replies
-
|
The crux is that the Now you added an Furthermore the exception is coming from a different component: Maybe you don't need the |
Beta Was this translation helpful? Give feedback.
-
|
Hello, @linkdotnet 1) my razor component: 2) Test for this component: 3) Error message which I have in this case:
Could you please provide example how I can to fix this error in my case ? |
Beta Was this translation helpful? Give feedback.
-
|
Yes I agree about that I render like this in my case: But I want to render this using But I don't know how to render it using
How I can render using |
Beta Was this translation helpful? Give feedback.
-
|
UPDATED with code that probably works :) (I actually have not tested it locally, just typing this out on my phone between meetings). @LasVegasIs, I would probably write my test in .razor file, but if you prefer C#, then something like this should work: var ctx = new TestContext();
var model = new SampleModel();
var editContext = new EditContext(model);
var dateFormat = "yyyy-MM-ddTHH:mm";
var minimumValue = DateTime.Now.AddDays(-2);
var minimumValueWithFormat = minimumValue.ToString(dateFormat);
var value = DateTime.Now;
var editForm = ctx.RenderComponent<CascadingValue<EditContext>>(ps => ps
.Add(p => p.value, editContext)
.AddChildContent<InputDateTime>(idtps => ipdtps.
.Add(p => p.MinimumValue, minimumValue)
.Add(p => p.Value, value)));
var cut = editForm.FindComponent<InputDateTime>();
// assertions ...The key is, as @linkdotnet mentions, that when you call An alternative is to use the change the root var ctx = new TestContext();
var model = new SampleModel();
var editContext = new EditContext(model);
var dateFormat = "yyyy-MM-ddTHH:mm";
var minimumValue = DateTime.Now.AddDays(-2);
var minimumValueWithFormat = minimumValue.ToString(dateFormat);
var value = DateTime.Now;
ctx.RendeTree.Add<CascadingValue<EditContext>>(ps => ps.Add(p => p.value, editContext));
var cut = RenderComponent<InputDateTime>(ps => ps
.Add(p => p.MinimumValue, minimumValue)
.Add(p => p.Value, value));
// assertions ...When you use add a component to the root Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
|
@egil for this cases: I have this error:
2. this error:
3. Also I tried to use: the same error which I had before that I think the second example the right way in this case but somehow I need to convert |
Beta Was this translation helpful? Give feedback.
-
|
Thanks 👍 , And I created test :) |
Beta Was this translation helpful? Give feedback.
UPDATED with code that probably works :) (I actually have not tested it locally, just typing this out on my phone between meetings).
@LasVegasIs, I would probably write my test in .razor file, but if you prefer C#, then something like this should work: