无法对继承对象的属性使用 nMock GetProperty 例程
对象设置期望时,我收到此错误。
当尝试对我模拟的继承自 MembershipUser: ContactRepositoryTests.UpdateTest : FailedSystem.InvalidProgramException: JIT 编译器遇到内部限制的
服务器堆栈跟踪: 在 MockObjectType1.ToString()
在 [0] 重新抛出异常: 在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) 在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(参考MessageData msgData,Int32类型) 在 System.Object.ToString() 在 NMock2.Internal.ExpectationBuilder.On(对象接收器)
这是我正在使用的工具...
VS2008 (SP1) 框架3.5 nUnit 2.4.8 nMock 2.0.0.44 Resharper 4.1
我不知道为什么会发生这种情况。 任何帮助,将不胜感激。
测试类...
[TestFixture]
public class AddressRepositoryTests
{
private Mockery m_Mockery;
private Data.IAddress m_MockDataAddress;
private IUser m_MockUser;
[SetUp]
public void Setup()
{
m_Mockery = new Mockery();
m_MockDataAddress = m_Mockery.NewMock<Data.IAddress>();
m_MockUser = m_Mockery.NewMock<IUser>();
}
[TearDown]
public void TearDown()
{
m_Mockery.Dispose();
}
[Test]
public void CreateTest()
{
string line1 = "unitTestLine1";
string line2 = "unitTestLine2";
string city = "unitTestCity";
int stateId = 1893;
string postalCode = "unitTestPostalCode";
int countryId = 223;
bool active = false;
int createdById = 1;
Expect.Once
.On(m_MockUser)
.GetProperty("Identity")
.Will(Return.Value(createdById));
Expect.Once
.On(m_MockDataAddress)
.Method("Insert")
.With(
line1,
line2,
city,
stateId,
postalCode,
countryId,
active,
createdById,
Is.Anything
)
.Will(Return.Value(null));
IAddressRepository addressRepository = new AddressRepository(m_MockDataAddress);
IAddress address = addressRepository.Create(
line1,
line2,
city,
stateId,
postalCode,
countryId,
active,
m_MockUser
);
Assert.IsNull(address);
}
}
用户类...
public interface IUser
{
int? Identity { get; set; }
int? CreatedBy { get; set; }
DateTime CreatedOn { get; set; }
int? ModifiedBy { get; set; }
DateTime? ModifiedOn { get; set; }
string UserName { get; }
object ProviderUserKey { get; }
string Email { get; set; }
string PasswordQuestion { get; }
string Comment { get; set; }
bool IsApproved { get; set; }
bool IsLockedOut { get; }
DateTime LastLockoutDate { get; }
DateTime CreationDate { get; }
DateTime LastLoginDate { get; set; }
DateTime LastActivityDate { get; set; }
DateTime LastPasswordChangedDate { get; }
bool IsOnline { get; }
string ProviderName { get; }
string ToString();
string GetPassword();
string GetPassword(string passwordAnswer);
bool ChangePassword(string oldPassword, string newPassword);
bool ChangePasswordQuestionAndAnswer(string password, string newPasswordQuestion, string newPasswordAnswer);
string ResetPassword(string passwordAnswer);
string ResetPassword();
bool UnlockUser();
}
public class User : MembershipUser, IUser
{
#region Public Properties
private int? m_Identity;
public int? Identity
{
get { return m_Identity; }
set
{
if (value <= 0)
throw new Exception("Address.Identity must be greater than 0.");
m_Identity = value;
}
}
public int? CreatedBy { get; set; }
private DateTime m_CreatedOn = DateTime.Now;
public DateTime CreatedOn
{
get { return m_CreatedOn; }
set { m_CreatedOn = value; }
}
public int? ModifiedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
#endregion Public Properties
#region Public Constructors
public User()
{ }
#endregion Public Constructors
}
地址类...
public interface IAddress
{
int? Identity { get; set; }
string Line1 { get; set; }
string Line2 { get; set; }
string City { get; set; }
string PostalCode { get; set; }
bool Active { get; set; }
int? CreatedBy { get; set; }
DateTime CreatedOn { get; set; }
int? ModifiedBy { get; set; }
DateTime? ModifiedOn { get; set; }
}
public class Address : IAddress
{
#region Public Properties
private int? m_Identity;
public int? Identity
{
get { return m_Identity; }
set
{
if (value <= 0)
throw new Exception("Address.Identity must be greater than 0.");
m_Identity = value;
}
}
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public bool Active { get; set; }
public int? CreatedBy { get; set; }
private DateTime m_CreatedOn = DateTime.Now;
public DateTime CreatedOn
{
get { return m_CreatedOn; }
set { m_CreatedOn = value; }
}
public int? ModifiedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
#endregion Public Properties
}
AddressRepository 类...
public interface IAddressRepository
{
IAddress Create(string line1, string line2, string city, int stateId, string postalCode, int countryId, bool active, IUser createdBy);
}
public class AddressRepository : IAddressRepository
{
#region Private Properties
private Data.IAddress m_DataAddress;
private Data.IAddress DataAddress
{
get
{
if (m_DataAddress == null)
m_DataAddress = new Data.Address();
return m_DataAddress;
}
set
{
m_DataAddress = value;
}
}
#endregion Private Properties
#region Public Constructor
public AddressRepository()
{ }
public AddressRepository(Data.IAddress dataAddress)
{
DataAddress = dataAddress;
}
#endregion Public Constructor
#region Public Methods
public IAddress Create(string line1, string line2, string city, int stateId, string postalCode, int countryId, bool active, IUser createdBy)
{
if (String.IsNullOrEmpty(line1)) throw new Exception("You must enter a Address Line 1 to register.");
if (String.IsNullOrEmpty(city)) throw new Exception("You must enter a City to register.");
if (stateId <= 0) throw new Exception("You must select a State to register.");
if (String.IsNullOrEmpty(postalCode)) throw new Exception("You must enter a Postal Code to register.");
if (countryId <= 0) throw new Exception("You must select a Country to register.");
DataSet dataSet = DataAddress.Insert(
line1,
line2,
city,
stateId,
postalCode,
countryId,
active,
createdBy.Identity,
DateTime.Now
);
return null;
}
#endregion Public Methods
}
DataAddress 类...
public interface IAddress
{
DataSet GetByAddressId (int? AddressId);
DataSet Update (int? AddressId, string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, Guid? ModifiedBy);
DataSet Insert (string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, int? CreatedBy, DateTime? CreatedOn);
}
public class Address : IAddress
{
public DataSet GetByAddressId (int? AddressId)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = database.GetStoredProcCommand("prAddress_GetByAddressId");
DataSet dataSet;
try
{
database.AddInParameter(dbCommand, "AddressId", DbType.Int32, AddressId);
dataSet = database.ExecuteDataSet(dbCommand);
}
catch (SqlException sqlException)
{
string callMessage = "prAddress_GetByAddressId " + "@AddressId = " + AddressId;
throw new Exception(callMessage, sqlException);
}
return dataSet;
}
public DataSet Update (int? AddressId, string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, Guid? ModifiedBy)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = database.GetStoredProcCommand("prAddress_Update");
DataSet dataSet;
try
{
database.AddInParameter(dbCommand, "AddressId", DbType.Int32, AddressId);
database.AddInParameter(dbCommand, "Address1", DbType.AnsiString, Address1);
database.AddInParameter(dbCommand, "Address2", DbType.AnsiString, Address2);
database.AddInParameter(dbCommand, "City", DbType.AnsiString, City);
database.AddInParameter(dbCommand, "StateId", DbType.Int32, StateId);
database.AddInParameter(dbCommand, "PostalCode", DbType.AnsiString, PostalCode);
database.AddInParameter(dbCommand, "CountryId", DbType.Int32, CountryId);
database.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
database.AddInParameter(dbCommand, "ModifiedBy", DbType.Guid, ModifiedBy);
dataSet = database.ExecuteDataSet(dbCommand);
}
catch (SqlException sqlException)
{
string callMessage = "prAddress_Update " + "@AddressId = " + AddressId + ", @Address1 = " + Address1 + ", @Address2 = " + Address2 + ", @City = " + City + ", @StateId = " + StateId + ", @PostalCode = " + PostalCode + ", @CountryId = " + CountryId + ", @IsActive = " + IsActive + ", @ModifiedBy = " + ModifiedBy;
throw new Exception(callMessage, sqlException);
}
return dataSet;
}
public DataSet Insert (string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, int? CreatedBy, DateTime? CreatedOn)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = database.GetStoredProcCommand("prAddress_Insert");
DataSet dataSet;
try
{
database.AddInParameter(dbCommand, "Address1", DbType.AnsiString, Address1);
database.AddInParameter(dbCommand, "Address2", DbType.AnsiString, Address2);
database.AddInParameter(dbCommand, "City", DbType.AnsiString, City);
database.AddInParameter(dbCommand, "StateId", DbType.Int32, StateId);
database.AddInParameter(dbCommand, "PostalCode", DbType.AnsiString, PostalCode);
database.AddInParameter(dbCommand, "CountryId", DbType.Int32, CountryId);
database.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
database.AddInParameter(dbCommand, "CreatedBy", DbType.Int32, CreatedBy);
database.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
dataSet = database.ExecuteDataSet(dbCommand);
}
catch (SqlException sqlException)
{
string callMessage = "prAddress_Insert " + "@Address1 = " + Address1 + ", @Address2 = " + Address2 + ", @City = " + City + ", @StateId = " + StateId + ", @PostalCode = " + PostalCode + ", @CountryId = " + CountryId + ", @IsActive = " + IsActive + ", @CreatedBy = " + CreatedBy + ", @CreatedOn = " + CreatedOn;
throw new Exception(callMessage, sqlException);
}
return dataSet;
}
}
I am getting this error when trying to set an expectation on an object I mocked that inherits from MembershipUser:
ContactRepositoryTests.UpdateTest : FailedSystem.InvalidProgramException: JIT Compiler encountered an internal limitation.
Server stack trace:
at MockObjectType1.ToString()
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref MessageData msgData, Int32 type)
at System.Object.ToString()
at NMock2.Internal.ExpectationBuilder.On(Object receiver)
Here are the tools I am using...
VS2008 (SP1)
Framework 3.5
nUnit 2.4.8
nMock 2.0.0.44
Resharper 4.1
I am at a loss as to why this would be happening. Any help would be appreciated.
Test Class...
[TestFixture]
public class AddressRepositoryTests
{
private Mockery m_Mockery;
private Data.IAddress m_MockDataAddress;
private IUser m_MockUser;
[SetUp]
public void Setup()
{
m_Mockery = new Mockery();
m_MockDataAddress = m_Mockery.NewMock<Data.IAddress>();
m_MockUser = m_Mockery.NewMock<IUser>();
}
[TearDown]
public void TearDown()
{
m_Mockery.Dispose();
}
[Test]
public void CreateTest()
{
string line1 = "unitTestLine1";
string line2 = "unitTestLine2";
string city = "unitTestCity";
int stateId = 1893;
string postalCode = "unitTestPostalCode";
int countryId = 223;
bool active = false;
int createdById = 1;
Expect.Once
.On(m_MockUser)
.GetProperty("Identity")
.Will(Return.Value(createdById));
Expect.Once
.On(m_MockDataAddress)
.Method("Insert")
.With(
line1,
line2,
city,
stateId,
postalCode,
countryId,
active,
createdById,
Is.Anything
)
.Will(Return.Value(null));
IAddressRepository addressRepository = new AddressRepository(m_MockDataAddress);
IAddress address = addressRepository.Create(
line1,
line2,
city,
stateId,
postalCode,
countryId,
active,
m_MockUser
);
Assert.IsNull(address);
}
}
User Class...
public interface IUser
{
int? Identity { get; set; }
int? CreatedBy { get; set; }
DateTime CreatedOn { get; set; }
int? ModifiedBy { get; set; }
DateTime? ModifiedOn { get; set; }
string UserName { get; }
object ProviderUserKey { get; }
string Email { get; set; }
string PasswordQuestion { get; }
string Comment { get; set; }
bool IsApproved { get; set; }
bool IsLockedOut { get; }
DateTime LastLockoutDate { get; }
DateTime CreationDate { get; }
DateTime LastLoginDate { get; set; }
DateTime LastActivityDate { get; set; }
DateTime LastPasswordChangedDate { get; }
bool IsOnline { get; }
string ProviderName { get; }
string ToString();
string GetPassword();
string GetPassword(string passwordAnswer);
bool ChangePassword(string oldPassword, string newPassword);
bool ChangePasswordQuestionAndAnswer(string password, string newPasswordQuestion, string newPasswordAnswer);
string ResetPassword(string passwordAnswer);
string ResetPassword();
bool UnlockUser();
}
public class User : MembershipUser, IUser
{
#region Public Properties
private int? m_Identity;
public int? Identity
{
get { return m_Identity; }
set
{
if (value <= 0)
throw new Exception("Address.Identity must be greater than 0.");
m_Identity = value;
}
}
public int? CreatedBy { get; set; }
private DateTime m_CreatedOn = DateTime.Now;
public DateTime CreatedOn
{
get { return m_CreatedOn; }
set { m_CreatedOn = value; }
}
public int? ModifiedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
#endregion Public Properties
#region Public Constructors
public User()
{ }
#endregion Public Constructors
}
Address Class...
public interface IAddress
{
int? Identity { get; set; }
string Line1 { get; set; }
string Line2 { get; set; }
string City { get; set; }
string PostalCode { get; set; }
bool Active { get; set; }
int? CreatedBy { get; set; }
DateTime CreatedOn { get; set; }
int? ModifiedBy { get; set; }
DateTime? ModifiedOn { get; set; }
}
public class Address : IAddress
{
#region Public Properties
private int? m_Identity;
public int? Identity
{
get { return m_Identity; }
set
{
if (value <= 0)
throw new Exception("Address.Identity must be greater than 0.");
m_Identity = value;
}
}
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public bool Active { get; set; }
public int? CreatedBy { get; set; }
private DateTime m_CreatedOn = DateTime.Now;
public DateTime CreatedOn
{
get { return m_CreatedOn; }
set { m_CreatedOn = value; }
}
public int? ModifiedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
#endregion Public Properties
}
AddressRepository Class...
public interface IAddressRepository
{
IAddress Create(string line1, string line2, string city, int stateId, string postalCode, int countryId, bool active, IUser createdBy);
}
public class AddressRepository : IAddressRepository
{
#region Private Properties
private Data.IAddress m_DataAddress;
private Data.IAddress DataAddress
{
get
{
if (m_DataAddress == null)
m_DataAddress = new Data.Address();
return m_DataAddress;
}
set
{
m_DataAddress = value;
}
}
#endregion Private Properties
#region Public Constructor
public AddressRepository()
{ }
public AddressRepository(Data.IAddress dataAddress)
{
DataAddress = dataAddress;
}
#endregion Public Constructor
#region Public Methods
public IAddress Create(string line1, string line2, string city, int stateId, string postalCode, int countryId, bool active, IUser createdBy)
{
if (String.IsNullOrEmpty(line1)) throw new Exception("You must enter a Address Line 1 to register.");
if (String.IsNullOrEmpty(city)) throw new Exception("You must enter a City to register.");
if (stateId <= 0) throw new Exception("You must select a State to register.");
if (String.IsNullOrEmpty(postalCode)) throw new Exception("You must enter a Postal Code to register.");
if (countryId <= 0) throw new Exception("You must select a Country to register.");
DataSet dataSet = DataAddress.Insert(
line1,
line2,
city,
stateId,
postalCode,
countryId,
active,
createdBy.Identity,
DateTime.Now
);
return null;
}
#endregion Public Methods
}
DataAddress Class...
public interface IAddress
{
DataSet GetByAddressId (int? AddressId);
DataSet Update (int? AddressId, string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, Guid? ModifiedBy);
DataSet Insert (string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, int? CreatedBy, DateTime? CreatedOn);
}
public class Address : IAddress
{
public DataSet GetByAddressId (int? AddressId)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = database.GetStoredProcCommand("prAddress_GetByAddressId");
DataSet dataSet;
try
{
database.AddInParameter(dbCommand, "AddressId", DbType.Int32, AddressId);
dataSet = database.ExecuteDataSet(dbCommand);
}
catch (SqlException sqlException)
{
string callMessage = "prAddress_GetByAddressId " + "@AddressId = " + AddressId;
throw new Exception(callMessage, sqlException);
}
return dataSet;
}
public DataSet Update (int? AddressId, string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, Guid? ModifiedBy)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = database.GetStoredProcCommand("prAddress_Update");
DataSet dataSet;
try
{
database.AddInParameter(dbCommand, "AddressId", DbType.Int32, AddressId);
database.AddInParameter(dbCommand, "Address1", DbType.AnsiString, Address1);
database.AddInParameter(dbCommand, "Address2", DbType.AnsiString, Address2);
database.AddInParameter(dbCommand, "City", DbType.AnsiString, City);
database.AddInParameter(dbCommand, "StateId", DbType.Int32, StateId);
database.AddInParameter(dbCommand, "PostalCode", DbType.AnsiString, PostalCode);
database.AddInParameter(dbCommand, "CountryId", DbType.Int32, CountryId);
database.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
database.AddInParameter(dbCommand, "ModifiedBy", DbType.Guid, ModifiedBy);
dataSet = database.ExecuteDataSet(dbCommand);
}
catch (SqlException sqlException)
{
string callMessage = "prAddress_Update " + "@AddressId = " + AddressId + ", @Address1 = " + Address1 + ", @Address2 = " + Address2 + ", @City = " + City + ", @StateId = " + StateId + ", @PostalCode = " + PostalCode + ", @CountryId = " + CountryId + ", @IsActive = " + IsActive + ", @ModifiedBy = " + ModifiedBy;
throw new Exception(callMessage, sqlException);
}
return dataSet;
}
public DataSet Insert (string Address1, string Address2, string City, int? StateId, string PostalCode, int? CountryId, bool? IsActive, int? CreatedBy, DateTime? CreatedOn)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = database.GetStoredProcCommand("prAddress_Insert");
DataSet dataSet;
try
{
database.AddInParameter(dbCommand, "Address1", DbType.AnsiString, Address1);
database.AddInParameter(dbCommand, "Address2", DbType.AnsiString, Address2);
database.AddInParameter(dbCommand, "City", DbType.AnsiString, City);
database.AddInParameter(dbCommand, "StateId", DbType.Int32, StateId);
database.AddInParameter(dbCommand, "PostalCode", DbType.AnsiString, PostalCode);
database.AddInParameter(dbCommand, "CountryId", DbType.Int32, CountryId);
database.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
database.AddInParameter(dbCommand, "CreatedBy", DbType.Int32, CreatedBy);
database.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
dataSet = database.ExecuteDataSet(dbCommand);
}
catch (SqlException sqlException)
{
string callMessage = "prAddress_Insert " + "@Address1 = " + Address1 + ", @Address2 = " + Address2 + ", @City = " + City + ", @StateId = " + StateId + ", @PostalCode = " + PostalCode + ", @CountryId = " + CountryId + ", @IsActive = " + IsActive + ", @CreatedBy = " + CreatedBy + ", @CreatedOn = " + CreatedOn;
throw new Exception(callMessage, sqlException);
}
return dataSet;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
刚刚解决了同样的问题。
请勿将 ToString() 添加到接口定义中。 它已经包含在每个对象中。
Just solved same problem.
DO NOT add ToString() to interface definition. It is already contained in every object.
有有效的犀牛模拟测试。
在rhino模拟测试中,您错过了对预期属性的调用(由Expect.call设置)
从查看代码来看,您似乎在nMock场景中犯了同样的错误
there are the working rhino mock tests.
In rhino mock test you were missing the call to property which was being expected (as set by Expect.call)
From by looking at the code, it seems you are doing the same mistake in nMock scenario