このトピックについて別の質問に書きました。
ただし、コードをリファクタリングして構成アクセスを削除したため、仕様に合格できるようになりました。とか、そう思いました。TestDriven.Net を使用して、Visual Studio 内から正常に実行されます。ただし、rake 中に mspec.exe ツールを使用して実行すると、シリアライゼーション例外で失敗します。そのため、スレッドに偽のセキュリティ資格情報を設定する以外は基本的に何もしない、完全に自己完結型の例を作成しました。このテストは TD.Net では問題なくパスしますが、mspec.exe では失敗します。誰か提案はありますか?
更新: 回避策を発見しました。問題を調査した結果、プリンシパル オブジェクトを含むアセンブリが mspec.exe と同じフォルダーにないことが原因のようです。mspec が新しい AppDomain を作成して仕様を実行する場合、その新しい AppDomain は、逆シリアル化するためにプリンシパル オブジェクトを含むアセンブリを読み込む必要があります。そのアセンブリは mspec EXE と同じフォルダーにないため、失敗します。アセンブリを mspec と同じフォルダーにコピーすると、正常に動作します。
私がまだ理解していないのは、ReSharper と TD.Net がテストを問題なく実行できる理由です。実際にテストを実行するために mspec.exe を使用しませんか?
using System;
using System.Security.Principal;
using System.Threading;
using Machine.Specifications;
namespace MSpecTest
{
[Subject(typeof(MyViewModel))]
public class When_security_credentials_are_faked
{
static MyViewModel SUT;
Establish context = SetupFakeSecurityCredentials;
Because of = () =>
SUT = new MyViewModel();
It should_be_initialized = () =>
SUT.Initialized.ShouldBeTrue();
static void SetupFakeSecurityCredentials()
{
Thread.CurrentPrincipal = CreatePrincipal(CreateIdentity());
}
static MyIdentity CreateIdentity()
{
return new MyIdentity(Environment.UserName, "None", true);
}
static MyPrincipal CreatePrincipal(MyIdentity identity)
{
return new MyPrincipal(identity);
}
}
public class MyViewModel
{
public MyViewModel()
{
Initialized = true;
}
public bool Initialized { get; set; }
}
[Serializable]
public class MyPrincipal : IPrincipal
{
private readonly MyIdentity _identity;
public MyPrincipal(MyIdentity identity)
{
_identity = identity;
}
public bool IsInRole(string role)
{
return true;
}
public IIdentity Identity
{
get { return _identity; }
}
}
[Serializable]
public class MyIdentity : IIdentity
{
private readonly string _name;
private readonly string _authenticationType;
private readonly bool _isAuthenticated;
public MyIdentity(string name, string authenticationType, bool isAuthenticated)
{
_name = name;
_isAuthenticated = isAuthenticated;
_authenticationType = authenticationType;
}
public string Name
{
get { return _name; }
}
public string AuthenticationType
{
get { return _authenticationType; }
}
public bool IsAuthenticated
{
get { return _isAuthenticated; }
}
}
}