Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,53 @@ public void WithDefaultJsonSettingsShouldNotThrowExceptionWithDefaultJsonSetting
.WithDefaultJsonSerializerSettings());
}

[Fact]
public void WithDefaultJsonSettingsShouldNotThrowExceptionAndPassAssertions()
{
MyController<MvcController>
.Instance()
.Calling(c => c.JsonAction())
.ShouldReturn()
.Json(result => result
.WithDefaultJsonSerializerSettings()
.AndAlso()
.Passing(json =>
{
Assert.Null(json.SerializerSettings);
}));
}

[Fact]
public void WithDefaultJsonSettingsShouldThrowExceptionWithNull()
{
Assert.Throws<InvocationAssertionException>(() =>
{
MyController<MvcController>
.Instance()
.Calling(c => c.JsonWithSpecificSettingsAction(null))
.ShouldReturn()
.Json(json => json
.WithDefaultJsonSerializerSettings());
});
}

[Fact]
public void WithDefaultJsonSettingsShouldThrowExceptionWithBuildInSerializationSettings()
{
var jsonSettings = new JsonSerializerSettings();

Test.AssertException<JsonResultAssertionException>(() =>
{
MyController<MvcController>
.Instance()
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
.ShouldReturn()
.Json(json => json
.WithDefaultJsonSerializerSettings());
},
"When calling JsonWithSpecificSettingsAction action in MvcController expected JSON result serializer settings to have DefaultContractResolver, but in fact found null.");
}

[Fact]
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithSameJsonSettings()
{
Expand All @@ -29,7 +76,96 @@ public void WithJsonSerializerSettingsShouldNotThrowExceptionWithSameJsonSetting
.Json(json => json
.WithJsonSerializerSettings(TestObjectFactory.GetJsonSerializerSettings()));
}


[Fact]
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithValidSettings()
{
var jsonSettings = TestObjectFactory.GetJsonSerializerSettings();

MyController<MvcController>
.Instance()
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
.ShouldReturn()
.Json(json => json
.WithJsonSerializerSettings(jsonSettings));
}

[Fact]
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithBuiltInSettings()
{
var jsonSettings = new JsonSerializerSettings();

MyController<MvcController>
.Instance()
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
.ShouldReturn()
.Json(json => json
.WithJsonSerializerSettings(jsonSettings));
}

[Fact]
public void WithJsonSerializerSettingsShouldNotThrowExceptionWithActionSettings()
{
var jsonSettings = new JsonSerializerSettings
{
CheckAdditionalContent = true,
NullValueHandling = NullValueHandling.Ignore
};

MyController<MvcController>
.Instance()
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
.ShouldReturn()
.Json(json => json
.WithJsonSerializerSettings(settings =>
{
settings.WithAdditionalContentChecking(true);
settings.WithNullValueHandling(NullValueHandling.Ignore);
}));
}

[Fact]
public void WithJsonSerializerSettingsShouldThrowExceptionWithActionSettings()
{
var jsonSettings = new JsonSerializerSettings
{
CheckAdditionalContent = true,
NullValueHandling = NullValueHandling.Ignore
};

Test.AssertException<JsonResultAssertionException>(
() =>
{
MyController<MvcController>
.Instance()
.Calling(c => c.JsonWithSpecificSettingsAction(jsonSettings))
.ShouldReturn()
.Json(json => json
.WithJsonSerializerSettings(settings =>
{
settings.WithAdditionalContentChecking(false);
settings.WithNullValueHandling(NullValueHandling.Ignore);
}));
},
"When calling JsonWithSpecificSettingsAction action in MvcController expected JSON result serializer settings to have disabled checking for additional content, but in fact it was enabled.");
}

[Fact]
public void WithJsonSerializerSettingsShouldThrowExceptionWithNull()
{
var jsonSettings = TestObjectFactory.GetJsonSerializerSettings();

Assert.Throws<InvocationAssertionException>(() =>
{
MyController<MvcController>
.Instance()
.Calling(c => c.JsonWithSpecificSettingsAction(null))
.ShouldReturn()
.Json(json => json
.WithJsonSerializerSettings(jsonSettings));
});
}

[Fact]
public void WithJsonSerializerSettingsShouldThrowExceptionWithDifferentJsonSettings()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionResultsTests.ViewTests
{
using System.Collections.Generic;
using Exceptions;
using Setups;
using Setups.Controllers;
using Setups.Models;
using System.Collections.Generic;
using Xunit;

public class ViewComponentTestBuilderTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,51 @@ public void WithViewEngineShouldNotThrowExceptionWithValidViewEngine()
.WithViewEngine(viewEngine));
}

[Fact]
public void WithViewEngineShouldNotThrowExceptionWithBuiltInView()
{
MyController<MvcController>
.Instance()
.Calling(c => c.View())
.ShouldReturn()
.View(view => view
.WithViewEngine(null));
}

[Fact]
public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineAndPassAssertions()
{
var viewEngine = TestObjectFactory.GetViewEngine();

MyController<MvcController>
.Instance()
.Calling(c => c.ViewWithViewEngine(viewEngine))
.ShouldReturn()
.View(result => result
.WithViewEngine(viewEngine)
.AndAlso()
.Passing(view =>
{
Assert.NotNull(view.ViewEngine);
Assert.IsAssignableFrom<IViewEngine>(view.ViewEngine);
Assert.True(typeof(CustomViewEngine) == view.ViewEngine.GetType());
}));
}

[Fact]
public void WithViewEngineShouldNotThrowExceptionWithBuiltInViewAndPassAssertions()
{
MyController<MvcController>
.Instance()
.Calling(c => c.View())
.ShouldReturn()
.View(result => result
.Passing(view =>
{
Assert.Null(view.ViewEngine);
}));
}

[Fact]
public void WithViewEngineShouldNotThrowExceptionWithNullViewEngine()
{
Expand Down Expand Up @@ -50,6 +95,25 @@ public void WithViewEngineShouldThrowExceptionWithInvalidViewEngine()
"When calling ViewWithViewEngine action in MvcController expected view result engine to be the same as the provided one, but instead received different result.");
}

[Fact]
public void WithViewEngineShouldThrowExceptionWithIncorrectValue()
{
var viewEngine = TestObjectFactory.GetViewEngine();

Test.AssertException<ViewResultAssertionException>(
() =>
{
MyController<MvcController>
.Instance()
.WithoutValidation()
.Calling(c => c.ViewWithViewEngine(viewEngine))
.ShouldReturn()
.View(view => view
.WithViewEngine(null));
},
"When calling ViewWithViewEngine action in MvcController expected view result engine to be the same as the provided one, but instead received different result.");
}

[Fact]
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngine()
{
Expand All @@ -61,6 +125,47 @@ public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngine()
.WithViewEngineOfType<CustomViewEngine>());
}

[Fact]
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngineAndCustomViewResult()
{
MyController<MvcController>
.Instance()
.Calling(c => c.CustomViewResult())
.ShouldReturn()
.View(view => view
.WithViewEngineOfType<CustomViewEngine>());
}

[Fact]
public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidTypeAndCustomViewResult()
{
Test.AssertException<ViewResultAssertionException>(() =>
{
MyController<MvcController>
.Instance()
.Calling(c => c.CustomViewResult())
.ShouldReturn()
.View(view => view
.WithViewEngineOfType<IViewEngine>());
},
"When calling CustomViewResult action in MvcController expected view result engine to be of IViewEngine type, but instead received CustomViewEngine.");
}

[Fact]
public void WithViewEngineOfTypeShouldThrowExceptionWithDifferentTypes()
{
Test.AssertException<ViewResultAssertionException>(() =>
{
MyController<MvcController>
.Instance()
.Calling(c => c.CustomViewResult())
.ShouldReturn()
.View(view => view
.WithViewEngineOfType<CompositeViewEngine>());
},
"When calling CustomViewResult action in MvcController expected view result engine to be of CompositeViewEngine type, but instead received CustomViewEngine.");
}

[Fact]
public void WithViewEngineOfTypeShouldThrowExceptionWithInvalidViewEngine()
{
Expand Down Expand Up @@ -107,6 +212,41 @@ public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineForPartials(
.WithViewEngine(viewEngine));
}

[Fact]
public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineForPartialsAndPassAssertions()
{
var viewEngine = TestObjectFactory.GetViewEngine();

MyController<MvcController>
.Instance()
.WithoutValidation()
.Calling(c => c.PartialViewWithViewEngine(viewEngine))
.ShouldReturn()
.PartialView(result => result
.WithViewEngine(viewEngine)
.AndAlso()
.Passing(partialView =>
{
Assert.NotNull(partialView.ViewEngine);
Assert.IsAssignableFrom<IViewEngine>(partialView.ViewEngine);
Assert.True(typeof(CustomViewEngine) == partialView.ViewEngine.GetType());
}));
}

[Fact]
public void WithViewEngineShouldNotThrowExceptionWithValidViewEngineForBuiltInPartials()
{
var viewEngine = TestObjectFactory.GetViewEngine();

MyController<MvcController>
.Instance()
.WithoutValidation()
.Calling(c => c.PartialView())
.ShouldReturn()
.PartialView(partialView => partialView
.WithViewEngine(null));
}

[Fact]
public void WithViewEngineShouldNotThrowExceptionWithNullViewEngineForPartials()
{
Expand All @@ -119,6 +259,25 @@ public void WithViewEngineShouldNotThrowExceptionWithNullViewEngineForPartials()
.WithViewEngine(null));
}

[Fact]
public void WithViewEngineShouldThrowExceptionWithCustomViewEngineForPartials()
{
var viewEngine = TestObjectFactory.GetViewEngine();

Test.AssertException<ViewResultAssertionException>(
() =>
{
MyController<MvcController>
.Instance()
.WithoutValidation()
.Calling(c => c.DefaultPartialView())
.ShouldReturn()
.PartialView(partialView => partialView
.WithViewEngine(viewEngine));
},
"When calling DefaultPartialView action in MvcController expected partial view result engine to be the same as the provided one, but instead received different result.");
}

[Fact]
public void WithViewEngineShouldThrowExceptionWithInvalidViewEngineForPartials()
{
Expand All @@ -136,6 +295,25 @@ public void WithViewEngineShouldThrowExceptionWithInvalidViewEngineForPartials()
"When calling PartialViewWithViewEngine action in MvcController expected partial view result engine to be the same as the provided one, but instead received different result.");
}

[Fact]
public void WithViewEngineForPartialsShouldThrowExceptionWithIncorrectValue()
{
var viewEngine = TestObjectFactory.GetViewEngine();

Test.AssertException<ViewResultAssertionException>(
() =>
{
MyController<MvcController>
.Instance()
.WithoutValidation()
.Calling(c => c.PartialViewWithViewEngine(viewEngine))
.ShouldReturn()
.PartialView(partialView => partialView
.WithViewEngine(null));
},
"When calling PartialViewWithViewEngine action in MvcController expected partial view result engine to be the same as the provided one, but instead received different result.");
}

[Fact]
public void WithViewEngineOfTypeShouldNotThrowExceptionWithValidViewEngineForPartials()
{
Expand Down
Loading