Telerik Forums
JustMock Forum
1 answer
393 views
Trying to find an example for mocking a repository that uses the Microsoft.Practices.EnterpriseLibrary.Data.

C# code.  Pretty sure I can create the Unit Test, just cannot determine how to create a Mock for the Database object from Microsoft.Practices.EnterpriseLibrary.Data.

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.}


Kaloyan
Telerik team
 answered on 09 Apr 2014
19 answers
132 views
Hello,

I want to mock a large interface (~630 methods). The following line takes several minutes and several GBytes of memory:

Dim mJob As IJobInterface = Mock.Create(Of IJobInterface)()

Am I doing something wrong or is there another way to mock that object. I need this mock for every unit test so I need a faster way to do this.

Cheers
Stefan
Telerik team
 answered on 11 Feb 2014
1 answer
115 views
How can i set a behavior like strict to a special or all MockContainer's instances?

Kaloyan
Telerik team
 answered on 24 Jan 2014
4 answers
197 views
An assertion on a mock where you use Args.Ignore() seems to ignore the extra "Occurs.Never()" condition i.e.

// Both of these pass
Mock.Assert(() => mock.Foo(null, null), Args.Ignore(), Occurs.Never());
Mock.Assert(() => mock.Foo(null, null), Args.Ignore());

If you remove the Args.Ignore() and explicitly specify the arguments, Occurs.Never() starts to actually be evaluated.
Kaloyan
Telerik team
 answered on 11 Dec 2013
5 answers
78 views
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));
    }
}

The above test passes, although it should fail.
Kaloyan
Telerik team
 answered on 06 Dec 2013
1 answer
76 views
If I have a MockingContainer and use Get<T> on it, and then try to Mock.Raise on an event of T, I receive an error: Unable to specify which event was deduced in the parameter. If I replace that with the same type generated by Mock.Create<T>, it works.
Kaloyan
Telerik team
 answered on 22 Nov 2013
1 answer
50 views
I have a simple event which does not derive from event args. It seems that there is no way to raise these events on mocked objects: -

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);
}

It appears that either (a) you must create a custom delegate rather than using EventHandler<T>, or (b) create a full class that inherits from EventArgs. If so - this is slightly cumbersome to use. It would be much better if there were no restriction that the type of T on EventHandler must derive from EventArgs.
Kaloyan
Telerik team
 answered on 22 Nov 2013
1 answer
34 views
I have a recursive mock. I'm doing an arrangement on the child mock, but this has an unintended side effect in that the call count of the parent mock is incremented - this should not be happening.

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
}
Kaloyan
Telerik team
 answered on 18 Nov 2013
7 answers
77 views
I've found an issue between Just Mock Lite when running unit tests that consistently fail, but only under Release build (presumably because of optimized code) and X64 builds with the XUnit test framework. I can't comment on whether this issue occurs under other test frameworks or not, but it definitely goes away under x86 and Debug builds. I have no idea why this happens. Cheers. Here's a sample: -

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());
    }
}

Here's the test failure message: -

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)
Stefan
Telerik team
 answered on 02 Sep 2013
12 answers
169 views
I'm calling a mock on multiple threads. Sometimes this works just fine. But sometimes the mock call fails with a System.ArgumentException "An item with the same key has already been added.". Here's the end of the stack trace: -

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    )

Now I'm not using any dictionaries in my code - is it possible that this is happening somewhere inside the mock?
Isaac Abraham
Top achievements
Rank 1
 answered on 14 Aug 2013
Narrow your results
Selected tags
Tags
+? more
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Mark
Top achievements
Rank 1
Yurii
Top achievements
Rank 1
Leland
Top achievements
Rank 2
Iron
Iron
Iron
Hon
Top achievements
Rank 1
Iron
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?