Telerik Forums
JustMock Forum
7 answers
129 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
448 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
181 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
122 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
113 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
783 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
103 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
104 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
142 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
9 answers
504 views
I'm probably doing something wrong here, but I can't figure out why Mock.Assert Occurs in my case isn't showing the mocked method called.  Here's the code, in brief:

Test code:
        [TestMethod]
      public void ProcessFilterResult_MailMessageDate_AfterLatestDBDate_ProcessesMessage_Test()
      {
            BounceHandler_Accessor target = new BounceHandler_Accessor();
            var data = Mock.Create<IDataAccess>();
            target._dal = data;
            Mock.Arrange( () => data.GetLatestEmailDate() ).Returns( DateTime.Now.AddDays( -7 ) );
            target._recordToDatabase = true;
 
            var result = Mock.Create<BounceFilterResult>();
 
            Cforge.Net.Mail.MailMessage msg = new Cforge.Net.Mail.MailMessage();
            msg.Date = new Cforge.Net.Mime.Headers.MailDateTime(DateTime.Now);
            msg.BodyText = @"X-ETC-MailingID: 00001
X-ETC-MailflowID: 10000";
 
            Mock.Arrange( () => result.MailMessage ).Returns( msg );
 
            target.ProcessFilterResult( result );
            Mock.Assert( () => data.RecordBouncedEmail( result, "00001", "10000" ), Occurs.Once() );
 
      }
the ProcessFilterResult checks some dates and a flag on the target, all of which pass. It uses some regex to find matches in the MailMessage.BodyText, which also match the asserted method call. Stepping through the code, I watch the result and data proxies match up to the Mocked objects and then watch the target._dal.RecordBouncedEmail() method call being called, but when I get back to the test code I still get the following exception. Telerik.JustMock.MockAssertionException was unhandled by user code
  Message=Expected call on the mock should be once, but it was called 0 time(s).
  Source=Telerik.JustMock Can anyone give me some idea what the problem might be?
Ricky
Telerik team
 answered on 30 Jul 2010
Narrow your results
Selected tags
Tags
+? more
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Anislav
Top achievements
Rank 6
Silver
Bronze
Bronze
Jianxian
Top achievements
Rank 1
Iron
Marco
Top achievements
Rank 3
Iron
Iron
Iron
Jim
Top achievements
Rank 2
Iron
Iron
Nurik
Top achievements
Rank 2
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?