Permalink
Browse files

Go HAM with Moq and NInject

  • Loading branch information...
paulcbetts committed Mar 24, 2012
1 parent f99bc5e commit 264adf243d3bcdde66b339358e74a8e43b2326ea
Showing with 77,157 additions and 28 deletions.
  1. +23 −0 ReactiveUI.Sample.Tests/ReactiveUI.Sample.Tests.csproj
  2. +37 −15 ReactiveUI.Sample.Tests/ViewModels/LoginViewModelTests.cs
  3. +6 −0 ReactiveUI.Sample.Tests/packages.config
  4. +3 −0 ReactiveUI.Sample.sln.DotSettings
  5. +1 −0 ReactiveUI.Sample/App.xaml.cs
  6. +24 −0 ReactiveUI.Sample/ReactiveUI.Sample.csproj
  7. +59 −2 ReactiveUI.Sample/ViewModels/AppViewModel.cs
  8. +20 −11 ReactiveUI.Sample/ViewModels/LoginViewModel.cs
  9. +22 −0 ReactiveUI.Sample/ViewModels/RepoViewModel.cs
  10. +4 −0 ReactiveUI.Sample/app.config
  11. +6 −0 ReactiveUI.Sample/packages.config
  12. +39 −0 packages/Moq.4.0.10827/License.txt
  13. BIN packages/Moq.4.0.10827/Moq.4.0.10827.nupkg
  14. BIN packages/Moq.4.0.10827/Moq.chm
  15. BIN packages/Moq.4.0.10827/lib/NET35/Moq.dll
  16. +5,768 −0 packages/Moq.4.0.10827/lib/NET35/Moq.xml
  17. BIN packages/Moq.4.0.10827/lib/NET40/Moq.dll
  18. +5,120 −0 packages/Moq.4.0.10827/lib/NET40/Moq.xml
  19. BIN packages/Moq.4.0.10827/lib/Silverlight4/Castle.Core.dll
  20. BIN packages/Moq.4.0.10827/lib/Silverlight4/Moq.Silverlight.dll
  21. +5,101 −0 packages/Moq.4.0.10827/lib/Silverlight4/Moq.Silverlight.xml
  22. BIN packages/Ninject.3.0.0-rc3/Ninject.3.0.0-rc3.nupkg
  23. BIN packages/Ninject.3.0.0-rc3/lib/net35/Ninject.dll
  24. +6,124 −0 packages/Ninject.3.0.0-rc3/lib/net35/Ninject.xml
  25. BIN packages/Ninject.3.0.0-rc3/lib/net40/Ninject.dll
  26. +6,124 −0 packages/Ninject.3.0.0-rc3/lib/net40/Ninject.xml
  27. BIN packages/Ninject.3.0.0-rc3/lib/net45/Ninject.dll
  28. +6,124 −0 packages/Ninject.3.0.0-rc3/lib/net45/Ninject.xml
  29. BIN packages/Ninject.3.0.0-rc3/lib/sl2/Ninject.dll
  30. +6,035 −0 packages/Ninject.3.0.0-rc3/lib/sl2/Ninject.xml
  31. BIN packages/Ninject.3.0.0-rc3/lib/sl3-wp/Ninject.dll
  32. +5,986 −0 packages/Ninject.3.0.0-rc3/lib/sl3-wp/Ninject.xml
  33. BIN packages/Ninject.3.0.0-rc3/lib/sl3/Ninject.dll
  34. +6,035 −0 packages/Ninject.3.0.0-rc3/lib/sl3/Ninject.xml
  35. BIN packages/Ninject.3.0.0-rc3/lib/sl4-windowsphone71/Ninject.dll
  36. +5,986 −0 packages/Ninject.3.0.0-rc3/lib/sl4-windowsphone71/Ninject.xml
  37. BIN packages/Ninject.3.0.0-rc3/lib/sl4/Ninject.dll
  38. +6,035 −0 packages/Ninject.3.0.0-rc3/lib/sl4/Ninject.xml
  39. BIN packages/Ninject.3.0.0-rc3/lib/sl5/Ninject.dll
  40. +6,035 −0 packages/Ninject.3.0.0-rc3/lib/sl5/Ninject.xml
  41. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/Ninject.Extensions.Logging.3.0.0-rc3.nupkg
  42. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/net35/Ninject.Extensions.Logging.dll
  43. +347 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/net35/Ninject.Extensions.Logging.xml
  44. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/net40/Ninject.Extensions.Logging.dll
  45. +347 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/net40/Ninject.Extensions.Logging.xml
  46. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/net45/Ninject.Extensions.Logging.dll
  47. +347 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/net45/Ninject.Extensions.Logging.xml
  48. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl2/Ninject.Extensions.Logging.dll
  49. +335 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl2/Ninject.Extensions.Logging.xml
  50. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl3-wp/Ninject.Extensions.Logging.dll
  51. +335 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl3-wp/Ninject.Extensions.Logging.xml
  52. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl3/Ninject.Extensions.Logging.dll
  53. +335 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl3/Ninject.Extensions.Logging.xml
  54. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl4-windowsphone71/Ninject.Extensions.Logging.dll
  55. +335 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl4-windowsphone71/Ninject.Extensions.Logging.xml
  56. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl4/Ninject.Extensions.Logging.dll
  57. +335 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl4/Ninject.Extensions.Logging.xml
  58. BIN packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl5/Ninject.Extensions.Logging.dll
  59. +335 −0 packages/Ninject.Extensions.Logging.3.0.0-rc3/lib/sl5/Ninject.Extensions.Logging.xml
  60. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/Ninject.Extensions.Logging.nlog2.3.0.0-rc3.nupkg
  61. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/net35/Ninject.Extensions.Logging.NLog2.dll
  62. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/net35/Ninject.Extensions.Logging.NLog2.xml
  63. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/net40/Ninject.Extensions.Logging.NLog2.dll
  64. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/net40/Ninject.Extensions.Logging.NLog2.xml
  65. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/net45/Ninject.Extensions.Logging.NLog2.dll
  66. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/net45/Ninject.Extensions.Logging.NLog2.xml
  67. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl2/Ninject.Extensions.Logging.NLog2.dll
  68. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl2/Ninject.Extensions.Logging.NLog2.xml
  69. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl3-wp/Ninject.Extensions.Logging.NLog2.dll
  70. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl3-wp/Ninject.Extensions.Logging.NLog2.xml
  71. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl3/Ninject.Extensions.Logging.NLog2.dll
  72. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl3/Ninject.Extensions.Logging.NLog2.xml
  73. BIN ...ct.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl4-windowsphone71/Ninject.Extensions.Logging.NLog2.dll
  74. +184 −0 ...ct.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl4-windowsphone71/Ninject.Extensions.Logging.NLog2.xml
  75. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl4/Ninject.Extensions.Logging.NLog2.dll
  76. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl4/Ninject.Extensions.Logging.NLog2.xml
  77. BIN packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl5/Ninject.Extensions.Logging.NLog2.dll
  78. +184 −0 packages/Ninject.Extensions.Logging.nlog2.3.0.0-rc3/lib/sl5/Ninject.Extensions.Logging.NLog2.xml
  79. BIN packages/Ninject.MockingKernel.3.0.0-rc3/Ninject.MockingKernel.3.0.0-rc3.nupkg
  80. BIN packages/Ninject.MockingKernel.3.0.0-rc3/lib/net35-full/Ninject.MockingKernel.dll
  81. +110 −0 packages/Ninject.MockingKernel.3.0.0-rc3/lib/net35-full/Ninject.MockingKernel.xml
  82. BIN packages/Ninject.MockingKernel.3.0.0-rc3/lib/net40-full/Ninject.MockingKernel.dll
  83. +110 −0 packages/Ninject.MockingKernel.3.0.0-rc3/lib/net40-full/Ninject.MockingKernel.xml
  84. BIN packages/Ninject.MockingKernel.3.0.0-rc3/lib/net45-full/Ninject.MockingKernel.dll
  85. +110 −0 packages/Ninject.MockingKernel.3.0.0-rc3/lib/net45-full/Ninject.MockingKernel.xml
  86. BIN packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl2/Ninject.MockingKernel.dll
  87. +110 −0 packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl2/Ninject.MockingKernel.xml
  88. BIN packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl3/Ninject.MockingKernel.dll
  89. +110 −0 packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl3/Ninject.MockingKernel.xml
  90. BIN packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl4/Ninject.MockingKernel.dll
  91. +110 −0 packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl4/Ninject.MockingKernel.xml
  92. BIN packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl5/Ninject.MockingKernel.dll
  93. +110 −0 packages/Ninject.MockingKernel.3.0.0-rc3/lib/sl5/Ninject.MockingKernel.xml
  94. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/Ninject.MockingKernel.Moq.3.0.0-rc3.nupkg
  95. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/net35-full/Ninject.MockingKernel.Moq.dll
  96. +157 −0 packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/net35-full/Ninject.MockingKernel.Moq.xml
  97. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/net40-full/Ninject.MockingKernel.Moq.dll
  98. +157 −0 packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/net40-full/Ninject.MockingKernel.Moq.xml
  99. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/net45-full/Ninject.MockingKernel.Moq.dll
  100. +157 −0 packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/net45-full/Ninject.MockingKernel.Moq.xml
  101. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl2/Ninject.MockingKernel.Moq.dll
  102. +89 −0 packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl2/Ninject.MockingKernel.Moq.xml
  103. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl3/Ninject.MockingKernel.Moq.dll
  104. +89 −0 packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl3/Ninject.MockingKernel.Moq.xml
  105. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl4/Ninject.MockingKernel.Moq.dll
  106. +157 −0 packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl4/Ninject.MockingKernel.Moq.xml
  107. BIN packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl5/Ninject.MockingKernel.Moq.dll
  108. +157 −0 packages/Ninject.MockingKernel.Moq.3.0.0-rc3/lib/sl5/Ninject.MockingKernel.Moq.xml
@@ -41,9 +41,32 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Rx-Compat_Unofficial.2.0.20304\lib\Net40\Microsoft.Reactive.Testing.dll</HintPath>
</Reference>
+ <Reference Include="Moq">
+ <HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
+ </Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.4.5.1\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
+ <Reference Include="Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.3.0.0-rc3\lib\net40\Ninject.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.Extensions.Logging.3.0.0-rc3\lib\net40\Ninject.Extensions.Logging.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.Extensions.Logging.NLog2, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.Extensions.Logging.nlog2.3.0.0-rc3\lib\net40\Ninject.Extensions.Logging.NLog2.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.MockingKernel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.MockingKernel.3.0.0-rc3\lib\net40-full\Ninject.MockingKernel.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.MockingKernel.Moq, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.MockingKernel.Moq.3.0.0-rc3\lib\net40-full\Ninject.MockingKernel.Moq.dll</HintPath>
+ </Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
</Reference>
@@ -1,10 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using FluentAssertions;
using Microsoft.Reactive.Testing;
+using Moq;
+using Ninject;
+using Ninject.MockingKernel;
+using Ninject.MockingKernel.Moq;
+using ReactiveUI.Routing;
using ReactiveUI.Testing;
using Xunit;
@@ -15,17 +21,23 @@ public class LoginViewModelTests : IEnableLogger
[Fact]
public void SuccessfulLoginShouldActuallyLogIn()
{
- var fixture = new LoginViewModel();
+ var mock = new MoqMockingKernel();
+ mock.Bind<ILoginViewModel>().To(typeof(LoginViewModel));
+
+ mock.Bind<Func<IObservable<Unit>>>()
+ .ToConstant<Func<IObservable<Unit>>>(() => Observable.Return(Unit.Default))
+ .Named("confirmUserPass");
+
+ var fixture = mock.Get<ILoginViewModel>();
fixture.User = "xpaulbettsx";
- fixture.Password = "62etmdtmwtmTM!";
+ fixture.Password = "theCorrectPassword";
fixture.Confirm.CanExecute(null).Should().BeTrue();
- fixture.Confirm.Execute(null);
- var result = MessageBus.Current.Listen<Tuple<string, string>>("login")
- .Timeout(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler)
- .First();
+ Tuple<string, string> result = null;
+ MessageBus.Current.Listen<Tuple<string, string>>("login").Subscribe(x => result = x);
+ fixture.Confirm.Execute(null);
result.Should().NotBeNull();
result.Item1.Should().Be(fixture.User);
@@ -35,7 +47,10 @@ public void SuccessfulLoginShouldActuallyLogIn()
[Fact]
public void BlankFieldsMeansNoLogin()
{
- var fixture = new LoginViewModel();
+ var mock = new MoqMockingKernel();
+ mock.Bind<ILoginViewModel>().To(typeof(LoginViewModel));
+
+ var fixture = mock.Get<ILoginViewModel>();
fixture.Confirm.CanExecute(null).Should().BeFalse();
@@ -54,20 +69,27 @@ public void BlankFieldsMeansNoLogin()
[Fact]
public void BadPasswordMeansErrorMessage()
{
- var fixture = new LoginViewModel();
+ var mock = new MoqMockingKernel();
+
+ mock.Bind<ILoginViewModel>().To(typeof(LoginViewModel));
+
+ mock.Bind<Func<IObservable<Unit>>>()
+ .ToConstant<Func<IObservable<Unit>>>(() => Observable.Throw<Unit>(new Exception("Bad Stuff")))
+ .Named("confirmUserPass");
+
+ var fixture = mock.Get<ILoginViewModel>();
fixture.User = "herpderp";
fixture.Password = "woefawoeifjwoefijwe";
fixture.Confirm.CanExecute(null).Should().BeTrue();
- fixture.Confirm.Execute(null);
- var result = fixture.ObservableForProperty(x => x.ErrorMessage)
- .Timeout(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler)
- .First();
+ string result = null;
+ fixture.ObservableForProperty(x => x.ErrorMessage).Subscribe(x => result = x.Value);
+ fixture.Confirm.Execute(null);
- this.Log().Info("Error Message: {0}", result.Value);
- result.Value.Should().NotBeNullOrEmpty();
+ this.Log().Info("Error Message: {0}", result);
+ result.Should().NotBeNullOrEmpty();
}
}
-}
+}
@@ -1,7 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="1.7.1" />
+ <package id="Moq" version="4.0.10827" />
<package id="Newtonsoft.Json" version="4.5.1" />
+ <package id="Ninject" version="3.0.0-rc3" />
+ <package id="Ninject.Extensions.Logging" version="3.0.0-rc3" />
+ <package id="Ninject.Extensions.Logging.nlog2" version="3.0.0-rc3" />
+ <package id="Ninject.MockingKernel" version="3.0.0-rc3" />
+ <package id="Ninject.MockingKernel.Moq" version="3.0.0-rc3" />
<package id="NLog" version="2.0.0.2000" />
<package id="RestSharp" version="102.7" />
<package id="xunit" version="1.9.0.1566" />
@@ -9,10 +9,13 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTIPLE_DECLARATION/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTLINE_TYPE_PARAMETER_CONSTRAINS/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTLINE_TYPE_PARAMETER_LIST/@EntryValue">False</s:Boolean>
+ <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">END_OF_LINE</s:String>
+ <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EXPLICIT_INTERNAL_MODIFIER/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EXPLICIT_PRIVATE_MODIFIER/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_ANONYMOUS_METHOD_BLOCK/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INDENT_CASE_FROM_SWITCH/@EntryValue">False</s:Boolean>
+ <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/OTHER_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=9b2b4c09_002Dbe0f_002D4b8e_002D8f1a_002Dcdccf074d45c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Private Fields/Methods/Properties"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;Kind Name="CONSTANT_FIELD" /&gt;&lt;Kind Name="METHOD" /&gt;&lt;Kind Name="PROPERTY" /&gt;&lt;Kind Name="EVENT" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String></wpf:ResourceDictionary>
@@ -4,6 +4,7 @@
using System.Data;
using System.Linq;
using System.Windows;
+using Ninject;
namespace ReactiveUI.Sample
{
@@ -40,9 +40,32 @@
<Reference Include="Microsoft.Practices.ServiceLocation">
<HintPath>..\packages\reactiveui-xaml.3.0.1\lib\Net4\Microsoft.Practices.ServiceLocation.dll</HintPath>
</Reference>
+ <Reference Include="Moq">
+ <HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
+ </Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.4.5.1\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
+ <Reference Include="Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.3.0.0-rc3\lib\net40\Ninject.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.Extensions.Logging.3.0.0-rc3\lib\net40\Ninject.Extensions.Logging.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.Extensions.Logging.NLog2, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.Extensions.Logging.nlog2.3.0.0-rc3\lib\net40\Ninject.Extensions.Logging.NLog2.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.MockingKernel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.MockingKernel.3.0.0-rc3\lib\net40-full\Ninject.MockingKernel.dll</HintPath>
+ </Reference>
+ <Reference Include="Ninject.MockingKernel.Moq, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\Ninject.MockingKernel.Moq.3.0.0-rc3\lib\net40-full\Ninject.MockingKernel.Moq.dll</HintPath>
+ </Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
</Reference>
@@ -100,6 +123,7 @@
<Compile Include="Models\GitHubUser.cs" />
<Compile Include="ViewModels\AppViewModel.cs" />
<Compile Include="ViewModels\LoginViewModel.cs" />
+ <Compile Include="ViewModels\RepoViewModel.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -2,17 +2,74 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using Microsoft.Practices.ServiceLocation;
+using Ninject;
using ReactiveUI.Routing;
namespace ReactiveUI.Sample.ViewModels
{
public class AppViewModel : ReactiveObject, IScreen
{
- public AppViewModel()
+ string _User;
+ public string User
{
- Router = new RoutingState();
+ get { return _User; }
+ set { this.RaiseAndSetIfChanged(x => x.User, value); }
+ }
+
+ string _Password;
+ public string Password
+ {
+ get { return _Password; }
+ set { this.RaiseAndSetIfChanged(x => x.Password, value); }
}
public RoutingState Router { get; protected set; }
+
+ public AppViewModel(IKernel testKernel)
+ {
+ Router = new RoutingState();
+
+ Kernel = testKernel ?? Kernel;
+ Kernel.Bind<IScreen>().ToSelf();
+
+ MessageBus.Current.Listen<Tuple<string, string>>("login").Subscribe(login => {
+ User = login.Item1;
+ Password = login.Item2;
+
+ Router.NavigateAndReset.Execute(Kernel.Get<IRepoViewModel>());
+ });
+
+ Router.Navigate.Execute(Kernel.Get<ILoginViewModel>());
+ }
+
+
+ //
+ // NInject static setup
+ //
+
+ static AppViewModel()
+ {
+ Kernel = new StandardKernel();
+ ServiceLocator.SetLocatorProvider(() => new NInjectServiceLocator());
+
+ Kernel.Bind<ILoginViewModel>().To<LoginViewModel>();
+ Kernel.Bind<IRepoViewModel>().To<RepoViewModel>();
+ }
+
+ public static IKernel Kernel { get; set; }
+ }
+
+ class NInjectServiceLocator : ServiceLocatorImplBase
+ {
+ protected override object DoGetInstance(Type serviceType, string key)
+ {
+ return AppViewModel.Kernel.Get(serviceType, key);
+ }
+
+ protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
+ {
+ return AppViewModel.Kernel.GetAll(serviceType);
+ }
}
}
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reactive;
using System.Reactive.Linq;
using System.Text;
+using Ninject;
using ReactiveUI;
using ReactiveUI.Routing;
using ReactiveUI.Sample.Models;
@@ -50,27 +52,34 @@ public string UrlPathSegment
public ReactiveAsyncCommand Confirm { get; protected set; }
- public LoginViewModel(ReactiveAsyncCommand confirm = null)
+ [Inject]
+ public LoginViewModel(IScreen hostScreen, [Named("confirmUserPass")] [Optional] Func<IObservable<Unit>> confirmUserPassMock = null)
{
+ HostScreen = hostScreen;
+
var canConfirm = this.WhenAny(x => x.User, x => x.Password,
(u, p) => !String.IsNullOrWhiteSpace(u.Value) && !String.IsNullOrWhiteSpace(p.Value));
- Confirm = confirm ?? new ReactiveAsyncCommand(canConfirm);
-
- var result = Confirm.RegisterAsyncObservable(_ => {
- var client = new RestClient("https://api.github.com");
- client.Authenticator = new HttpBasicAuthenticator(User, Password);
+ Confirm = new ReactiveAsyncCommand(canConfirm);
- return client.RequestAsync<GitHubUser>(new RestRequest("user"))
- .Do(res => this.Log().Info("User's URL: {0}", res.Data.url))
- .Select(res => new Tuple<string, string>(User, Password));
- });
+ var confirmFunc = confirmUserPassMock ?? TestUserNameAndPassword;
+ var result = Confirm.RegisterAsyncObservable(_ => confirmFunc());
- MessageBus.Current.RegisterMessageSource(result, "login");
+ MessageBus.Current.RegisterMessageSource(result.Select(res => new Tuple<string, string>(User, Password)), "login");
Confirm.ThrownExceptions
.Select(x => x.Message)
.ToProperty(this, x => x.ErrorMessage);
}
+
+ IObservable<Unit> TestUserNameAndPassword()
+ {
+ var client = new RestClient("https://api.github.com");
+ client.Authenticator = new HttpBasicAuthenticator(User, Password);
+
+ return client.RequestAsync<GitHubUser>(new RestRequest("user"))
+ .Do(res => this.Log().Info("User's URL: {0}", res.Data.url))
+ .Select(_ => Unit.Default);
+ }
}
}
@@ -0,0 +1,22 @@
+using ReactiveUI.Routing;
+
+namespace ReactiveUI.Sample.ViewModels
+{
+ public interface IRepoViewModel : IRoutableViewModel
+ {
+ }
+
+ public class RepoViewModel : ReactiveObject, IRepoViewModel
+ {
+ public string UrlPathSegment {
+ get { return "repos"; }
+ }
+
+ public IScreen HostScreen { get; protected set; }
+
+ public RepoViewModel(IScreen hostScreen)
+ {
+ HostScreen = hostScreen;
+ }
+ }
+}
@@ -9,6 +9,10 @@
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
+ <dependentAssembly>
+ <assemblyIdentity name="Moq" publicKeyToken="69f491c39445e920" culture="neutral" />
+ <bindingRedirect oldVersion="0.0.0.0-4.0.10827.0" newVersion="4.0.10827.0" />
+ </dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
+ <package id="Moq" version="4.0.10827" />
<package id="Newtonsoft.Json" version="4.5.1" />
+ <package id="Ninject" version="3.0.0-rc3" />
+ <package id="Ninject.Extensions.Logging" version="3.0.0-rc3" />
+ <package id="Ninject.Extensions.Logging.nlog2" version="3.0.0-rc3" />
+ <package id="Ninject.MockingKernel" version="3.0.0-rc3" />
+ <package id="Ninject.MockingKernel.Moq" version="3.0.0-rc3" />
<package id="NLog" version="2.0.0.2000" />
<package id="RestSharp" version="102.7" />
</packages>
Oops, something went wrong.

0 comments on commit 264adf2

Please sign in to comment.