or
[TestFixture]
public
abstract
class
TestBase
{
public
IClientService ClientService;
[TestFixtureSetUp]
public
void
InitFixture()
{
ClientService = Mock.Create<IClientService>();
}
}
[TestFixture]
public
class
ClientServiceTests : TestBase
{
[Test]
public
void
Test()
{
Mock.Arrange(() => ClientService.Retrieve(Arg.AnyLong))
.Returns(() =>
new
Client());
var client = ClientService.Retrieve(0);
Assert.IsNotNull(client);
}
}
public class A
{
public static bool TryGet<
T
>(string key, out T obj)
{
....
}
}
public
static
object
AMethod(
string
key)
{
object
values;
if
(A.TryGet(key,
out
values))
{
return
values;
}
return
null
;
}
Enum.GetValues
). But I have no idea how to set up the mock for the ToString() method of the Enum object.public
static
void
TestThis
(ListControl ctrl, Type enumType, Enum enumItem=
null
)
{
int
[] itemValues = Enum.GetValues(enumType).ToArray<
int
>();
string
[] itemNames = Enum.GetNames(enumType);
string
s = enumItem.ToString();
}
Mock.SetupStatic(
typeof
(Enum), Behavior.Strict);
Mock.Arrange(() => Enum.GetValues(Arg.IsAny<Type>())).Returns(
new
int
[] { 1, 2, 3 });
Mock.Arrange(() => Enum.GetNames(Arg.IsAny<Type>())).Returns(
new
string
[] {
"1"
,
"2"
,
"3"
});
Enum e = Mock.Create<Enum>();
Mock.Arrange(() => e.ToString()).IgnoreArguments().IgnoreInstance().Returns(
"2"
);
//---
//--- in that case null is returned instead of "2"; compiler never steps into the mocked "returns"
TestEnum
e = Mock.Create<TestEnum>();
Mock.Arrange(() => e.ToString()).IgnoreArguments().IgnoreInstance().Returns(
"2"
);
//---
DropDownList ddlTestList = new DropDownList();
TestThis(ddlTestList, typeof(TestEnum), TestEnum.Empty
)
using
System;
using
System.Threading;
using
System.Threading.Tasks;
using
System.Linq;
using
Microsoft.VisualStudio.TestTools.UnitTesting;
using
Telerik.JustMock;
namespace
TestMockingStatic
{
public
class
ClassWithStatic
{
public
static
int
NumGot = 0;
public
static
int
GetANumber(
int
foo,
int
bar)
{
throw
new
NotImplementedException();
}
}
[TestClass]
public
class
UnitTest1
{
public
void
GetValue()
{
Task.Factory.StartNew(() =>
{
ClassWithStatic.NumGot = ClassWithStatic.GetANumber(5, 10);
});
}
[TestMethod]
public
void
TestMethod1()
{
Mock.SetupStatic<ClassWithStatic>(Behavior.Strict);
Mock.Arrange(() => ClassWithStatic.GetANumber(Arg.AnyInt, Arg.AnyInt)).Returns(42);
Assert.AreEqual(42, ClassWithStatic.GetANumber(5, 10),
"Mocked Method did not return expected result"
);
GetValue();
SpinWait.SpinUntil(() => ClassWithStatic.NumGot > 0, 20000);
Assert.AreEqual(42, ClassWithStatic.NumGot,
"Mocked method called via TASK, did not return correct value"
);
}
}
}
service.Arrange<
Employee
>(emp => emp.LoadByName(Arg.AnyString)).Returns(data.employees.Where(e => e.Name == /* whatever the value matched by Arg.AnyString was */));
public Guid CreateNewSite(String siteUrl, string newSite)
{
using
(SPSite site =
new
SPSite(siteUrl))
{
if
(!site.AllWebs.Names.Contains(newSite))
{
return
site.AllWebs.Add(newSite, newSite,
"New Website"
, 1033,
"STS#1"
,
true
,
false
).ID;
}
}
SPSite fakeSite = Mock.Create<SPSite>();
Mock.Arrange(() => fakeSite.AllWebs.Names).ReturnsCollection<
string
>(
new
[] {
"nonExistsWeb"
});
Mock.Arrange(() => fakeSite.AllWebs.Add(Arg.AnyString, Arg.AnyString, Arg.AnyString, (
uint
)Arg.AnyInt, Arg.AnyString, Arg.AnyBool, Arg.AnyBool).ID).Returns(SomeGuid);
Guid actual = spListCreator.CreateNewSite("http://local", "SomeSite");
Hello, I have some questions about unit testing with Justmock.
I have this very simple method (Share Point code):
Code:
1 question.public
Guid CreateNewSite(String siteUrl,
string
newSite)
{
if
(!(
string
.IsNullOrEmpty(newSite) ||
string
.IsNullOrEmpty(siteUrl)))
{
using
(SPSite site =
new
SPSite(siteUrl))
{
if
(!site.AllWebs.Names.Contains(newSite))
{
return
site.AllWebs.Add(newSite, newSite,
"New Website"
, 1033,
"STS#1"
,
true
,
false
).ID;
}
}
}
return
Guid.Empty;
}
public
void
AddSplistItemToList(SPWeb web, Guid listId, String listItemTitle)
{
if
(!(listId == Guid.Empty || listId == Guid.Empty ||
string
.IsNullOrEmpty(listItemTitle)))
{
SPList list = web.Lists[listId];
SPListItem item = list.Items.Add();
item[
"Title"
] = listItemTitle;
item.Update();
}
}
Because the AddSplistItemToList is void, I want to test, that item["Title"] would set correctly (value from listItemTitle parameter). How I can test and fake this with Justmock?