Umbraco-CMS
Umbraco-CMS copied to clipboard
Unit test failure in Umbraco10 but work in Umbraco9
Which exact Umbraco version are you using? For example: 9.0.1 - don't just write v9
v10.01
Bug summary
Hi All,
I write a simple function of unit test as below, it work on Umbraco v8 without error
[Fact]
public void MyCodeTest() {
var parentAlias = "parentAlias";
var testParentCode = new Mock<IPublishedContent>();
var testParentCodeProperty = new Mock<IPublishedProperty>();
var cultureCodeElement = new List<IPublishedElement>();
testParentCodeProperty.Setup(s => s.Alias).Returns(parentAlias);
testParentCodeProperty.Setup(s => s.GetValue(null, null)).Returns(cultureCodeElement);
testParentCodeProperty.Setup(s => s.HasValue(null, null)).Returns(true);
testParentCode.Setup(s => s.GetProperty(parentAlias)).Returns(testParentCodeProperty.Object);
var testSiteNode = new Mock<IWebNode>();
testSiteNode.Setup(x => x.FromNodePath(It.IsAny<string>())).Returns(testParentCode.Object);
var resultNode = testSiteNode.Object.FromNodePath(parentAlias);
var result = resultNode?.Value(parentAlias) as IEnumerable<IPublishedElement>;
result.Should().NotBeNull();
}
when I copy this code into Umbraco 10, when go to var result = resultNode?.Value(parentAlias) as IEnumerable<IPublishedElement>;
, it throw exception :
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'Umbraco.Extensions.FriendlyPublishedContentExtensions' threw an exception.
Source=Umbraco.Web.Common
StackTrace:
at Umbraco.Extensions.FriendlyPublishedContentExtensions.get_PublishedValueFallback()
at Umbraco.Extensions.FriendlyPublishedContentExtensions.Value(IPublishedContent content, String alias, String culture, String segment, Fallback fallback, Object defaultValue)
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
ArgumentNullException: Value cannot be null. (Parameter 'provider')
Can anyone tell me the different between two version
Specifics
No response
Steps to reproduce
The main code
[Fact]
public void MyCodeTest() {
var parentAlias = "parentAlias";
var testParentCode = new Mock<IPublishedContent>();
var testParentCodeProperty = new Mock<IPublishedProperty>();
var cultureCodeElement = new List<IPublishedElement>();
testParentCodeProperty.Setup(s => s.Alias).Returns(parentAlias);
testParentCodeProperty.Setup(s => s.GetValue(null, null)).Returns(cultureCodeElement);
testParentCodeProperty.Setup(s => s.HasValue(null, null)).Returns(true);
testParentCode.Setup(s => s.GetProperty(parentAlias)).Returns(testParentCodeProperty.Object);
var testSiteNode = new Mock<IWebNode>();
testSiteNode.Setup(x => x.FromNodePath(It.IsAny<string>())).Returns(testParentCode.Object);
var resultNode = testSiteNode.Object.FromNodePath(parentAlias);
var result = resultNode?.Value(parentAlias) as IEnumerable<IPublishedElement>;
result.Should().NotBeNull();
}
Just assume it have no error to return value
Expected result / actual result
Just assume it have no error to return value
Hi there @laputaworkuk!
Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.
We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.
- We'll assess whether this issue relates to something that has already been fixed in a later version of the release that it has been raised for.
- If it's a bug, is it related to a release that we are actively supporting or is it related to a release that's in the end-of-life or security-only phase?
- We'll replicate the issue to ensure that the problem is as described.
- We'll decide whether the behavior is an issue or if the behavior is intended.
We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.
Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:
Hi @laputaworkuk,
Thank you for reaching out - and sorry it took us so long to respond 🙈
These kinds of questions really do belong at the forum: https://our.umbraco.com/forum
You may also find some useful info at the docs site: https://docs.umbraco.com/umbraco-cms/implementation/unit-testing
And finally Dennis Adolfi has a lot of good stuff to get you started as well - for example: https://adolfi.dev/blog/unit-testing-in-umbraco-9-10-x/
Hi there, I need to write some unit tests but we still got this error. I use these packages and this code. nothing else . umbraco v12.2.0 . xunit v2.5.3 . NSubstitute v5.1.0
var content = Substitute.For<IPublishedContent>();
content.Value<string>("title").Returns("Title");
cc: @sekmenhuseyin @aochmann