or
public
interface
IDao
{
// throws an exception on duplicate key
void
Insert(
string
key,
string
value);
// throws an exception if key not available
void
Update(
string
key,
string
value);
// throws an exception of key not found
string
FindByKey(
string
key);
}
public
interface
IService
{
string
Get(
string
key);
void
Set(
string
key,
string
value);
// should insert or update the value for the given key
void
Save(
string
key,
string
value);
}
public
class
ServiceImpl : IService
{
private
readonly
IDao _dao;
public
ServiceImpl(IDao dao)
{
_dao = dao;
}
public
string
Get(
string
key)
{
return
_dao.FindById(key);
}
public
void
Set(
string
key,
string
value)
{
_dao.Insert(key, value);
}
public
void
Save(
string
key,
string
value)
{
var value = Get(key);
if
(
null
== value)
{
Set(key, value);
}
else
{
_dao.Update(key, value);
}
}
}
[Test]
public
void
SaveShouldCallInsertOnNonExistingKey()
{
var key =
"key"
;
var value =
"value"
;
var dao = Mock.Create<IDao>();
dao.Arrange(x => x.Insert(Arg.AnyString,Arg.AnyString)).MustBeCalled();
var service = Mock.Create<ServiceImpl>(Behaviour.CallOriginal, dao);
service.Arrange(x => x.Get(Arg.AnyString)).Returns((
string
)
null
).MustBeCalled();
service.Save(key, value);
Mock.AssertAll(dao);
Mock.AssertAll(service);
}
MSomeClass.AllInstances.GetError = (instance) =>
{
return string.Empty;
};
public interface IViewModel
{
string DisplayName { get; }
Brush BackgroundBrush { get; }
}
<
UserControl
x:Class
=
"View"
[...]>
<
Grid
x:Name
=
"LayoutRoot"
Background
=
"{Binding Path=BackgroundBrush}"
>
<
Border
BorderBrush
=
"Black"
BorderThickness
=
"1"
>
<
TextBlock
Text
=
"{Binding Path=DisplayName}"
/>
</
Border
>
</
Grid
>
</
UserControl
>
[TestMethod]
[Asynchronous]
public
void
BindingTest_BackgroundBrushIsSet()
{
var target =
new
View();
var datacontextMock = Mock.Create<IViewModel>();
var expectedBackground =
new
SolidColorBrush(Colors.Purple);
target.DataContext = datacontextMock;
Mock.Arrange(() => datacontextMock.BackgroundBrush).Returns(expectedBackground);
Mock.Arrange(() => datacontextMock.DisplayName).Returns(
string
.Empty);
AsyncWaitForFrameworkElementLoaded(target);
AssertCallback
(() =>
{
var layoutRoot = target.FindName<Grid>(
"LayoutRoot"
);
Assert.AreEqual(expectedBackground, layoutRoot.Background);
});
}