or
[TestMethod]
public
void
Load_Default_Profile_For_New_User()
{
var userProfileService = Mock.Create<UserProfileServiceBase>(Behavior.Loose);
Mock.Arrange(() => userProfileService.GetByUserId(Arg.AnyInt)).IgnoreInstance().Returns<
int
>(id => {
return
null
; });
Mock.Arrange(() => userProfileService.Insert(Arg.IsAny<UserProfile>())).IgnoreInstance().Returns(
true
);
Assert.IsTrue(Mock.IsProfilerEnabled,
"Profiler not enabled"
);
var userService =
new
PaXtreme.Services.UserService();
var userProfile = userService.GetUserProfile(1);
Assert.IsNotNull(userProfile);
Assert.IsFalse(
string
.IsNullOrEmpty(userProfile.ProfileData));
}
Test method SPM150.Test.LogBenchmarkValueChangeTest.Test0020LogTransactionCalledWhenValuesDifferentCorrectParmetersVersion1 threw exception:
Telerik.JustMock.MockException: Profiler must be enabled to mock/assert target SPM150ControllerTestBase.get_AssetsServiceMock() method.
[TestClass]
public
class
SPM150ControllerTestBase
{
public
IAssetsService AssetsServiceMock {
get
;
set
; }
public
INavigationService NavigationServiceMock {
get
;
set
; }
public
ISPM150Service SPM150ServiceMock {
get
;
set
; }
public
IGlobalData GlobalDataMock {
get
;
set
; }
public
SPM150Controller Target {
get
;
set
; }
[TestInitialize]
public
void
TestInit()
{
AssetsServiceMock = Mock.Create<IAssetsService>();
SPM150ServiceMock = Mock.Create<ISPM150Service>();
NavigationServiceMock = Mock.Create<INavigationService>();
GlobalDataMock = Mock.Create<IGlobalData>();
Target = Mock.Create<SPM150Controller>(Behavior.CallOriginal,
new
object
[] { AssetsServiceMock, SPM150ServiceMock, NavigationServiceMock, GlobalDataMock,
null
});
}
}
[TestClass]
public
class
LogBenchmarkValueChangeTest : SPM150ControllerTestBase
{
private
const
string
PassedSiteNumber =
"12345"
;
private
const
string
PassedUserName =
"Hi Bob!"
;
private
const
string
PassedPrefix =
"EQ Item Update"
;
[TestMethod]
public
void
Test0020LogTransactionCalledWhenValuesDifferentCorrectParmetersVersion1()
{
var beforeValue = 100.0;
var afterValue = 1002.0;
var expectedActionString =
"EQ List Item Benchmark value changed from [100.0] to [1002.0]"
;
AssetsServiceMock.Arrange(asset => asset.LogTransaction(Arg.AnyString, Arg.AnyString, Arg.AnyString)).OccursOnce();
Target.LogBenchmarkValueChange(PassedSiteNumber, afterValue, PassedUserName, PassedPrefix, beforeValue);
AssetsServiceMock.Assert();
Mock.Assert(() => AssetsServiceMock.LogTransaction(
Arg.Matches<
string
>(site => site == PassedSiteNumber),
Arg.Matches<
string
>(user => user == PassedUserName),
Arg.Matches<
string
>(actionString => actionString == expectedActionString)));
}
}
EntitiesModel dbContext =
new
EntitiesModel();
if
(dbContext !=
null
)
{
EntityName myEntity = EntityName();
using
(OAConnection connection = dbContext.Connection)
{
using
(OACommand command = connection.CreateCommand())
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText =
"SpName"
;
using
(DbDataReader dataReader = command.ExecuteReader())
{
if
(dataReader.HasRows)
{
IEnumerable<EntityName> entityname = dbContext.Translate<EntityName>(dataReader);
foreach
EntityName c
in
entityname
{
myEntity = dbContext.AttachCopy<EntityName>(c);
}
}
dataReader.NextResult();
if
(dataReader.HasRows)
{
while
(dataReader.Read())
{
this
._myField = dataReader.GetString(1);
}
}
if
(!dataReader.IsClosed)
dataReader.Close();
}
}
}
}
public
void
Work(
int
count)
{
if
(count == 1)
{
MyEvent +=
new
MyHandler(handleEvent);
}
}
public class Class1
{
public Class1()
{
Parameter = new VMParameter() { Token = 21 };
}
internal VMParameter Parameter { get; set; }
}
internal class VMParameter
{
public int Token { get; set; }
}
// Unit Test
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var c = Mock.Create<
Class1
>(Behavior.CallOriginal);
var p = Mock.Create<
VMParameter
>();
p.Token = 1;
Mock.Arrange(() => c.Parameter).Returns(p);
Assert.AreEqual(c.Parameter.Token, 1);
}
}
[TestMethod]
public
void
ShouldAssertMockedGetAllCategories()
{
NorthwindEntities entities = Mock.Create<NorthwindEntities>();
Category category =
new
Category()
{
CategoryName =
"Beer"
};
List<Category> allCategories =
new
List<Category>
{
category
};
// Arrange
Mock.Arrange(() => entities.Categories).ReturnsCollection(allCategories);
// Act
List<Category> categoriesList
= entities.Categories.ToList();
// Assert
Assert.AreEqual(1, categoriesList.Count);
Assert.AreSame(category, categoriesList[0]);
}
Again, everything worked like a charm.
Since I don't access a ObjectContext directly I tried to make things a little bit real world like.
[TestMethod]
public
void
ShouldAssertMockedGetAllCategories()
{
NorthwindEntities entities = Mock.Create<NorthwindEntities>();
Category category =
new
Category()
{
CategoryName =
"Beer"
};
List<Category> allCategories =
new
List<Category>
{
category
};
// Arrange
Mock.Arrange(() => entities.Categories).IgnoreInstance().ReturnsCollection(allCategories);
// Act
//Some instance of some class calls the following lines of code somewhere, somehow. =)
NorthwindEntities ne =
new
NorthwindEntities();
List<Category> categoriesList = ne.Categories.ToList();
// Assert
Assert.AreEqual(1, categoriesList.Count);
Assert.AreSame(category, categoriesList[0]);
}
Test passed. \o/
Oops! I forget to call Dispose(). Let's first try the default way to do that.
[TestMethod]
public
void
ShouldAssertMockedGetAllCategories()
{
NorthwindEntities entities = Mock.Create<NorthwindEntities>();
Category category =
new
Category()
{
CategoryName =
"Beer"
};
List<Category> allCategories =
new
List<Category>
{
category
};
// Arrange
Mock.Arrange(() => entities.Categories).IgnoreInstance().ReturnsCollection(allCategories);
// Act
//Some instance of some class calls the following lines of code somewhere, somehow. =)
List<Category> categoriesList;
using
(NorthwindEntities ne =
new
NorthwindEntities())
{
categoriesList = ne.Categories.ToList();
}
// Assert
Assert.AreEqual(1, categoriesList.Count);
Assert.AreSame(category, categoriesList[0]);
}
Uh-oh. An Exception was thrown at the end of using block.
Even if I call Dispose() directly (instead of using the using statement), test fails for the same reason.
[TestMethod]
public
void
ShouldAssertMockedGetAllCategories()
{
NorthwindEntities entities = Mock.Create<NorthwindEntities>();
Category category =
new
Category()
{
CategoryName =
"Beer"
};
List<Category> allCategories =
new
List<Category>
{
category
};
// Arrange
Mock.Arrange(() => entities.Categories).IgnoreInstance().ReturnsCollection(allCategories);
// Act
//Some instance of some class calls the following lines of code somewhere, somehow. =)
NorthwindEntities ne =
new
NorthwindEntities();
List<Category> categoriesList = ne.Categories.ToList();
ne.Dispose();
// Assert
Assert.AreEqual(1, categoriesList.Count);
Assert.AreSame(category, categoriesList[0]);
}
Am I missing something here?
Thanks in advance.