Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewbagdatasupport #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<ItemGroup>
<Reference Include="ImpromptuInterface">
<HintPath>..\packages\ImpromptuInterface.6.0.6\lib\net40\ImpromptuInterface.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
Expand Down
1 change: 1 addition & 0 deletions TestStack.FluentMVCTesting.Mvc3/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ImpromptuInterface" version="6.0.6" targetFramework="net40" />
<package id="Microsoft.AspNet.Mvc" version="3.0.20105.1" targetFramework="net40" />
<package id="Microsoft.AspNet.Razor" version="1.0.20105.408" targetFramework="net40" />
<package id="Microsoft.AspNet.WebPages" version="1.0.20105.408" targetFramework="net40" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NUnit.Framework;
//
using NUnit.Framework;
//
using TestStack.FluentMVCTesting.Tests.TestControllers;

namespace TestStack.FluentMVCTesting.Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
//
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Net;
using System.Web.Mvc;
//
using NUnit.Framework;
//
using TestStack.FluentMVCTesting.Tests.TestControllers;

namespace TestStack.FluentMVCTesting.Tests
Expand Down
5 changes: 4 additions & 1 deletion TestStack.FluentMVCTesting.Tests/ModelErrorTestTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System;//

using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
//
using NSubstitute;
//
using NUnit.Framework;

namespace TestStack.FluentMVCTesting.Tests
Expand Down
3 changes: 2 additions & 1 deletion TestStack.FluentMVCTesting.Tests/ModelTestTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
//
using NUnit.Framework;

namespace TestStack.FluentMVCTesting.Tests
{
Expand Down
69 changes: 68 additions & 1 deletion TestStack.FluentMVCTesting.Tests/ViewResultTestTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Web.Mvc;
//
using System;
using System.Collections.Generic;
using System.Web.Mvc;
//
using NUnit.Framework;

namespace TestStack.FluentMVCTesting.Tests
Expand All @@ -19,6 +23,69 @@ public void Setup()
_viewResultTest = new ViewResultTest(_viewResult, new ViewTestController());
}

[Test]
public void Check_string_message_key_on_viewbag()
{
_viewResult.ViewBag.Message = "Hello View Data Message";
Assert.That(_viewResultTest.WithViewBag<string>("Message"), Is.EqualTo("Hello View Data Message"));
}

[Test]
public void Check_int_message_key_on_viewbag()
{
_viewResult.ViewBag.Page = 1;
Assert.That(_viewResultTest.WithViewBag<int>("Page"), Is.EqualTo(1));
}

[Test]
public void Check_wrong_message_member_name_passed_missing_member_message_for_viewbag()
{
_viewResult.ViewBag.Message = "Hello View Data Message";

var exception = Assert.Throws<MissingMemberException>(() =>
_viewResultTest.WithViewBag<string>("Message2")
);

Assert.That(exception.Message, Is.EqualTo("Member 'ViewBag.Message2' not found."));
}


[Test]
public void Check_string_message_key_on_viewdata()
{
_viewResult.ViewData["Message"] = "Hello View Data Message";
Assert.That(_viewResultTest.WithViewData<string>("Message"), Is.EqualTo("Hello View Data Message"));
}

[Test]
public void Check_int_message_key_on_viewdata()
{
_viewResult.ViewData["Page"] = 1;
Assert.That(_viewResultTest.WithViewData<int>("Page"), Is.EqualTo(1));
}

[Test]
public void Check_wrong_message_key_passed_missing_message_key_for_viewdata()
{
_viewResult.ViewData["Message"] = "Hello View Data Message";

var exception = Assert.Throws<KeyNotFoundException>(() =>
_viewResultTest.WithViewData<string>("Message2")
);

Assert.That(exception.Message, Is.EqualTo("Exception with ViewData, 'Message2' key not found"));
}

[Test]
public void Check_row_count_zero_for_viewdata()
{
var exception = Assert.Throws<KeyNotFoundException>(() =>
_viewResultTest.WithViewData<string>("Message")
);

Assert.That(exception.Message, Is.EqualTo("Exception with ViewData, 'Message' key not found"));
}

[Test]
public void Check_the_type_of_model()
{
Expand Down
3 changes: 2 additions & 1 deletion TestStack.FluentMvcTesting/ControllerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
//
using System;
using System.Linq.Expressions;
using System.Web.Mvc;

Expand Down
1 change: 1 addition & 0 deletions TestStack.FluentMvcTesting/ControllerResultTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//
using System;
using System.Linq.Expressions;
using System.Net;
Expand Down
3 changes: 2 additions & 1 deletion TestStack.FluentMvcTesting/Exceptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
//
using System;

namespace TestStack.FluentMVCTesting
{
Expand Down
1 change: 1 addition & 0 deletions TestStack.FluentMvcTesting/ModelErrorTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
1 change: 1 addition & 0 deletions TestStack.FluentMvcTesting/ModelTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
Expand Down
3 changes: 3 additions & 0 deletions TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<ItemGroup>
<Reference Include="ImpromptuInterface">
<HintPath>..\packages\ImpromptuInterface.6.0.6\lib\net40\ImpromptuInterface.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
Expand Down
32 changes: 32 additions & 0 deletions TestStack.FluentMvcTesting/ViewResultTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
//
using ImpromptuInterface;

namespace TestStack.FluentMVCTesting
{
Expand All @@ -13,6 +18,33 @@ public ViewResultTest(ViewResultBase viewResult, Controller controller)
_viewResult = viewResult;
_controller = controller;
}

public TMember WithViewBag<TMember>(string psMemberName)
{
TMember member = default(TMember);
var target = _viewResult.ViewBag;

IEnumerable<string> memberNames = Impromptu.GetMemberNames(_viewResult.ViewBag, true);
if ( !memberNames.Contains(psMemberName) )
throw new MissingMemberException("ViewBag",psMemberName);

member = Impromptu.InvokeGet(target, psMemberName);

return member;
}
public TProperty WithViewData<TProperty>(string psKeyName)
{
if (_viewResult.ViewData == null || _viewResult.ViewData.Count <= 0 || !_viewResult.ViewData.ContainsKey(psKeyName))
throw new KeyNotFoundException(string.Format("Exception with ViewData, '{0}' key not found",psKeyName));

var target = _viewResult.ViewData;
TProperty propertyValue = default(TProperty);

if (target != null)
propertyValue = (TProperty)target[psKeyName];

return propertyValue;
}

public ModelTest<TModel> WithModel<TModel>() where TModel : class
{
Expand Down
1 change: 1 addition & 0 deletions TestStack.FluentMvcTesting/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ImpromptuInterface" version="6.0.6" targetFramework="net40" />
<package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Razor" version="2.0.20715.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net40" />
Expand Down