or
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Text;
05.
using
System.Threading.Tasks;
06.
using
Microsoft.Practices.EnterpriseLibrary.Data;
07.
using
System.Data;
08.
using
System.Data.Common;
09.
// Other using statements for Models
10.
11.
namespace
Test.Repository
12.
{
13.
public
class
MyDatabaseRepo : IMyDatabaseRepo
14.
{
15.
// Microsoft.Practices.EnterpriseLibrary.Data 'Database' object
16.
readonly
Database _database;
17.
18.
public
MyDatabaseRepo(Database dataSource)
19.
{
20.
_database = dataSource;
21.
}
22.
23.
public
int
InsertNewCustomer(ClientEntity client)
24.
{
25.
int
ClientRecordId = 0;
26.
using
(DbCommand dbCmd = _da.GetStoredProcCommand(
"usp_InsertNewCustomers"
))
27.
{
28.
_database.AddInParameter(dbCmd,
"@Address1"
, DbType.String, client.Address1);
29.
_database.AddInParameter(dbCmd,
"@Address2"
, DbType.String, client.Address2);
30.
_database.AddInParameter(dbCmd,
"@City"
, DbType.String, client.City);
31.
_database.AddInParameter(dbCmd,
"@State"
, DbType.String, client.State);
32.
_database.AddInParameter(dbCmd,
"PostalCode"
, DbType.String, client.PostalCode);
33.
_database.AddOutParameter(dbCmd,
"@recordID"
, DbType.Int32, 0);
34.
35.
using
(IDataReader reader = _database.ExecuteReader(dbCmd))
36.
{
37.
reader.Read();
38.
ClientRecordId = Convert.ToInt32(dbCmd.Parameters[
"@recordID"
].Value);
39.
}
40.
}
41.
42.
return
ClientRecordId;
43.
}
44.
}
45.
}
Dim mJob As IJobInterface = Mock.Create(Of IJobInterface)()
// Both of these pass
Mock.Assert(() => mock.Foo(
null
,
null
), Args.Ignore(), Occurs.Never());
Mock.Assert(() => mock.Foo(
null
,
null
), Args.Ignore());
public
interface
IFoo
{
IBar GetBar(
string
x);
}
public
interface
IBar
{
void
DoBar(
int
x);
}
public
class
Tests
{
[Fact]
public
void
First()
{
var foo = Mock.Create<IFoo>();
foo.GetBar(
"a"
).DoBar(1);
Mock.Assert(() => foo.GetBar(
"b"
).DoBar(1));
}
}
public
interface
IFoo
{
event
EventHandler<
string
> Message;
}
[Fact]
public
void
Sample()
{
bool
fired =
false
;
var create = Mock.Create<IFoo>();
create.Message += (o,e) => fired =
true
;
Mock.Raise(() => create.Message +=
null
,
"Test"
);
//bang
Assert.True(fired);
}
public
interface
IMyInterface
{
IOther GetOther();
}
public
interface
IOther
{
int
Foo(
string
test);
}
[Fact]
public
void
TestMethod()
{
var myInterface = Mock.Create<IMyInterface>();
Mock.Assert(() => myInterface.GetOther(), Occurs.Never());
Mock.Arrange(() => myInterface.GetOther().Foo(
null
)).IgnoreArguments().Returns(1);
Mock.Assert(() => myInterface.GetOther(), Occurs.Never());
// BANG
}
public
class
MyClass
{
private
readonly
MockClass generator;
public
MyClass(MockClass generator)
{
this
.generator = generator;
}
public
void
Foo(Object someData)
{
generator.Bar();
}
}
public
interface
MockClass
{
void
Bar();
}
public
class
TestClass
{
private
readonly
MockClass generator;
private
readonly
MyClass myClass;
public
TestClass()
{
generator = Mock.Create<MockClass>();
myClass =
new
MyClass(generator);
}
[Fact]
public
void
X64Failure()
{
myClass.Foo(
null
);
Mock.Assert(() => generator.Bar());
}
}
X64Failure has failed
Occurrence expectation failed. Expected calls
in
range [1, (any)]. Actual calls: 0
\x8\x2.\x19\x3.\x7
\x12\x1A\x3(String )
\x2.\x2
Assert(Nullable`1 \x2, Nullable`1 \x2, Int32 \x18\x3)
Telerik.JustMock.Core.MocksRepository
\x13\x2(\x2 \x2, Boolean \x11\x2, Occurs \x7\x2)
Assert(Object \x2, Expression \x2, Occurs \x7\x2, Boolean \x11\x2)
Telerik.JustMock.Mock.\x3
\x2()
\x2.\x2
\x2(Action \x2)
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at . . (MethodInfo )
at . . (MockInvocation )
at . .Intercept(MockInvocation )
at . .Intercept(MockInvocation )
at . .Intercept(IInvocation )