Telerik Forums
JustMock Forum
5 answers
279 views
I can't seem to get Mock.Arrange working with a dictionary parameter. I've included some sample code below. I was expecting ret to be equal to "test" but it is coming back as nothing. Have I got the syntax wrong in the Mock.Arrange() call?


<TestClass()> _
Public Class MyTests
 
    <TestMethod()> _
    Public Sub GetValue_ValueReturnedFromDatabase()
        
        Dim ds As IDataService = Mock.Create(Of IDataService)()
        Mock.Arrange(Function() ds.GetValue(Arg.AnyString, Arg.IsAny(Of Dictionary(Of String, Object)))).Returns("test")
        Dim l As New LD(ds)
 
        Dim ret As String = l.GetValue("xxx")
 
        Assert.AreEqual("test", ret)
    End Sub
 
     
End Class
 
 
Public Interface IDataService
    Function GetValue(ByVal SQL As String, ByVal Params As Dictionary(Of String, Object)) As String
End Interface
 
Public Class DS
    Implements IDataService
    Public Function GetValue(ByVal SQL As String, ByVal Params As Dictionary(Of String, Object)) As String Implements IDataService.GetValue
        Return "anything"
    End Function
End Class
 
Public Class LD
    Private _DataService As IDataService
 
    Public Sub New(ByVal DataService As IDataService)
        _DataService = DataService
    End Sub
 
    Public Function GetValue(ByVal Name As String)
        Dim params As New Dictionary(Of String, Object)
        Dim ret As String = _DataService.GetValue("Some SQL", params)
        Return ret
    End Function
End Class
Chris
Telerik team
 answered on 20 Aug 2010
7 answers
132 views
Hi.

My projects depend on a big common.dll that have many dependencies. Amongst others, it references System.Environment, but I'm not sure when and what. Currently I have set up the windows environment variables so that the tests execute, but I don't want to depend on that when running my unit tests for the future.

Can I mock out System.Environment, run a test case and then inspect intercepted calls to it so that I can create a sensible mocking behaviour?

----

Also, sometimes, especially from legacy components, I get a rather sophisticated blob. For example from an automated fire central. In order to testrun my application, I normally need to be connected to the real fire central.

I would like to turn these integration tests scenarios into unit tests. And I'm tired of creating test scenarios manually on the fire central.

The approach that makes the most sense to me, is that I would be connected the real HW and mock the fire central communication class, then I would run a complete scenario and then inspect the mock for what calls were being made to it and what the response from the fire central was, including events. Then I wish to easily arrange a mock that reproduces this behaviour for the future without the fire central connected.

Is there anything in JustMock that facilitates this?
Ricky
Telerik team
 answered on 20 Aug 2010
5 answers
462 views
I'm attempting to mock an ObjectStateEntry from EF4. Unfortunately, it fails at runtime when I create the mocked object. This is my first time using JustMock (although I've done some very basic tests successfully) so I could have something setup/configured incorrectly. I'd appreciate some help getting this to work.

var mockedEntry = Mock.Create<ObjectStateEntry>();

This compiles just fine but has a runtime error:

TestCase 'Foo'
failed: System.TypeLoadException : Method 'set_EntityKey' in type 'ObjectStateEntry_Proxy_1f0c098e8cf2440f9eb3e6c8fa6abb46' from assembly 'Telerik.JustMock, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b221631f7271365' does not have an implementation.
    at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
    at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
    at System.Reflection.Emit.TypeBuilder.CreateType()
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\TypeEmitter.cs(287,0): at ‚.‘.–()
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\Proxy.cs(87,0): at Telerik.JustMock.DynamicProxy.Proxy.–()
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\ProxyFactory.cs(78,0): at Telerik.JustMock.DynamicProxy.ProxyFactory.Create()
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\Fluent\FluentProxy.cs(72,0): at Telerik.JustMock.DynamicProxy.Fluent.FluentProxy.NewInstance()
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicProxy\Proxy.cs(81,0): at Telerik.JustMock.DynamicProxy.Proxy.Create(Type target, Action`1 action)
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockObject.cs(149,0): at ..(Type target, Container container, Object[] args)
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockObject.cs(123,0): at ..(Type target, Container container, Object[] args, Boolean profilerEnabled)
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockObject.cs(97,0): at ..(Type target, Behavior behavior, Boolean static, Object[] args)
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockObject.cs(44,0): at ..get_Instance()
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs(485,0): at Telerik.JustMock.Mock.Create(Type target, Behavior mode, Object[] args)
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs(427,0): at Telerik.JustMock.Mock.Create(Type target, Object[] args)
    c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs(416,0): at Telerik.JustMock.Mock.Create[T]()
    ExtensionMethods\ObjectStateEntryExtensionsTests.cs(39,0): at EG.Framework.EntityFramework.Tests.ExtensionMethods.ObjectStateEntryExtensionsTests.Does_UpdateAllEntityAuditFieldsAndCancelDeletes_Properly_Cancel_Deletes()
 
0 passed, 1 failed, 0 skipped, took 1.00 seconds (NUnit 2.5.5).

I'm working towards the following test:

// Arrange
var mockedEntry = Mock.Create<ObjectStateEntry>();
var entity = new ObjectWithAuditFields();
 
Mock.Arrange(() => mockedEntry.State).Returns(EntityState.Deleted);
Mock.Arrange(() => mockedEntry.Entity).Returns(entity);
Mock.Arrange(() => mockedEntry.EntityKey).IgnoreArguments();
Mock.Arrange(() => mockedEntry.ChangeState(EntityState.Modified))
    .DoInstead(() => Mock.Arrange(() => mockedEntry.State).Returns(EntityState.Modified));
 
// Act
//mockedEntry.UpdateAllEntityAuditFieldsAndCancelDeletes(44);
mockedEntry.ChangeState(EntityState.Modified);
 
// Assert
Assert.IsTrue(mockedEntry.State == EntityState.Modified);

... so that I can perform tests on the commented-out line. However, since I can't even create the mocked object, this is rather difficult...
Chris
Telerik team
 answered on 20 Aug 2010
4 answers
197 views
Hi,

I've been trying to mock calls the the System.Environment class in MsCorlib.

I followed the guidelines in the documentation:
  • I created the class with the MockClass attribute:
    [TestClass, MockClass]
    public class UnitTest1
  • I tried to use the partial method:
    Mock.Partial<Environment>().For<string>((x) => Environment.GetFolderPath(Arg.IsAny<Environment.SpecialFolder>()));
But this doesn't feel right (the lambda uses an instance but the class is static) and it does not compile for the same reason: cannot use static types as type arguments.

Is there a way to mock static mscorlib classes? (Environment.GetFolderPath)

Thanks!
DenisCL
DenisCL
Top achievements
Rank 1
 answered on 17 Aug 2010
3 answers
128 views
Hi.

My test projects have data files as part of the solution. To avoid dependencies on the file system on the CI server, I embed them in the test assembly as manifest resources. The problem is when I intercept File.Open calls using JustMock. What do I do? What do I return? Do I pass it a Mock<FileInfo>? Since "File.Open()" is mentioned about as often as "DateTime.Now" to show the strengths of TypeMock and JustMock, what are good practices/strategies to employ when dealing with it?

http://stackoverflow.com/questions/3466355/how-can-i-pass-a-solution-resource-as-a-filestream-object-to-an-intercepted-file
Ricky
Telerik team
 answered on 17 Aug 2010
1 answer
121 views
Some sample test code, any one

I am new to JockMock tool in silverlight and need a sample unit test case for ViewModel in MVVM
Below is method that is part of ViewModel

 

public void Save()

 

{

 

 

    string serializedTradeObjects = string.Empty;

 

 

 

    if (_reviewedTradeContext.HasChanges) // RIA.Context

 

    {

 

 

        if (_reviewedTradeContext.EntityContainer.GetChanges().ModifiedEntities.Count > 0)

 

    {

 

 

        this.IsBusy = true;

 

 

 

        var modifications = _reviewedTradeContext.EntityContainer.GetChanges().ModifiedEntities;

 

 

 

        List<Trade> tradeList = new List<Trade>();

 

 

 

        foreach (Entity trade in modifications)

 

        {

            tradeList.Add((

 

Trade)trade);

 

        }

        serializedTradeObjects = tradeList.Serialize();

     }

 

 

        InvokeOperation<string> reviewTradesInvokeOperation = _reviewedTradeContext.UploadTrades(serializedTradeObjects, String.Empty, String.Empty);

 

    reviewTradesInvokeOperation.Completed +=

 

new EventHandler(reviewTradesInvokeOperation_Completed);

 

    }

}

Can some one provide me the Unit Test case in Jock Mock

Cheers,
Suman

 

Ricky
Telerik team
 answered on 13 Aug 2010
4 answers
806 views
I'm trying to mock the static System.IO namespace methods in C#, so I have calls like this:

            Mock.SetupStatic<File>();
            Mock.Arrange( () => File.AppendText(Arg.AnyString)).DoNothing();

because I want to stop the filesystem from writing anything.  When I do so, however, I receive the following error:
'Telerik.JustMock.Expectations.FuncExpectation<System.IO.StreamWriter>' does not contain a definition for 'DoNothing' and no extension method 'DoNothing' accepting a first argument of type 'Telerik.JustMock.Expectations.FuncExpectation<System.IO.StreamWriter>' could be found (are you missing a using directive or an assembly reference?)

In a different test method in the same class using a static class of my own, I am able to make that call without any difficulty.

What am I missing?

cori
Top achievements
Rank 1
 answered on 13 Aug 2010
1 answer
109 views
I tried to compare use of different mock frameworks in F#. While Rhino.Mocks and NSubstitute succeeded with simple tests (I only tried basic things), both Typemock and JustMock failed, and Moq failed in one scenario and succeeded in the other one.

I blogged about my tests:
http://bloggingabout.net/blogs/vagif/archive/2010/08/04/mock-framework-challenges-in-f.aspx

I know this can not be used as a quality measure: F# is a new and very different language. But since it gains larger attention, I believe it's important to provide support for it.

If Telerik is interested in getting F# support, I can do more tests and investigations. Right now there are simple things that don't work, as you can see from my blog post.
Ricky
Telerik team
 answered on 09 Aug 2010
4 answers
111 views
I watched the video of JustMock. I am surprised for what JustMock can do. Actually I immediately thought it could be a run time AOP. But then I read the following words about JustMock:
"This approach (Profiling API) is quite powerful but has its own disadvantages like the hard deployment. You need to have it integrated with your Visual Studio and also with your testing framework and also in some cases its performance is worse compared to how the dynamic proxy approach works." (Digested from http://www.telerik.com/community/forums/justmock/general-discussions/is-justmock-a-quot-profiler-quot.aspx ).

But in http://msdn.microsoft.com/en-us/magazine/cc188743.aspx , it is said Profiling API 
" not suited for deployment in a production environment. The reason is that it's based on the Profiling API, which after being registered circumvents the CLR security infrastructure. Plus it prevents use of other profilers which are seeking to perform real performance analysis on the app."

It seems in theory, JustMock can be used in a standalone application, out of visual studio.

So, I have related questions for Telerik to confirm:
1. Does JustMock library only run when it is integrated with Visual Studio?
2. Is there a way to run it at run time in a standalone application?
3. Does Telerik have a plan to make it an AOP, not only a mock?

Thank you in advance,

Ying
Ricky
Telerik team
 answered on 03 Aug 2010
5 answers
149 views
I have a problem where concrete class is not able to be mocked. I see recently in an other post that the problem was corrected just after the build was released but I'm not able to see if this is correct because no nightly build is available now. Is nightly build just available for registered users?
Ricky
Telerik team
 answered on 02 Aug 2010
Narrow your results
Selected tags
Tags
+? more
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Rob
Top achievements
Rank 3
Iron
Iron
Iron
Atul
Top achievements
Rank 1
Iron
Iron
Iron
Alexander
Top achievements
Rank 1
Veteran
Iron
Serkan
Top achievements
Rank 1
Iron
Shawn
Top achievements
Rank 1
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?