Telerik Forums
JustMock Forum
7 answers
73 views
Hello everybody,
I face to such a problem that that raged in my head. So I need your help to solve it.
I've a user control and a method CalculatePreviewArea like that:
public partial class Control_Inside : UserControl
{
//some properties...  
//(private) method1()
//(private) method2()
 private void CalculatePreviewArea(out double width, out double height)
        {
          
           //some logic here...         
           //FillArea is dependency property of User Control
            width = FillArea.Width;
            height =FillArea.Height;
             
            double Sys_Width =  SystemParameters.WorkArea.Width;
            double Sys_Height = SystemParameters.WorkArea.Height;
            Window window = Application.Current.MainWindow;
            Point location_From_Screen = window.PointToScreen(new Point(0.0, 0.0));
             
 
            if (location_From_Screen.X + width - Fill_Area.X >= Sys_Width)
                width =Sys_Width + (location_From_Screen.X - Fill_Area.X);
             
            //....
           }
Now I've to unit test for this method (CalculatePreviewArea) using JustMock. I created a mock user control , I also set FillArea value successfull, but there is no way to access Application.Current.Windows. When I debug the test,it always return null. I need your helps to test this, thanks.
Kaloyan
Telerik team
 answered on 31 May 2013
3 answers
162 views
Hi,

I'd like to mock internal class in test project. I added InternalsVisibleToAttribute to JustMock and my test assembly, but in Silverlight I'm getting this error: TypeLoadException: Access is denied: 'Xtd.SomeClass`2+SomeSubClass[Xtd.Period,Xtd.Container]'.
w System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
w System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
w System.Reflection.Emit.TypeBuilder.CreateType()
w Telerik.JustMock.DynamicProxy.TypeEmitter.CreateType()
w Telerik.JustMock.DynamicProxy.Proxy.CreateType()
w Telerik.JustMock.DynamicProxy.ProxyFactory.Create()
w Telerik.JustMock.DynamicProxy.Fluent.FluentProxy.NewInstance()
w Telerik.JustMock.DynamicProxy.Proxy.Create(Type target, Action`1 action)
w Telerik.JustMock.MockManager.CreateProxy(Type targetType, Container container)
w Telerik.JustMock.MockManager.CreateInstance(Container container)
w Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean static)
w Telerik.JustMock.MockManager.CreateInstance()
w Telerik.JustMock.Mock.Create(Type target, Behavior behavior, Object[] args)
w Telerik.JustMock.Mock.Create[T](Behavior behavior, Object[] args)
w Xtd.Tests.SomeClassTests.Initialize()

Everything works in WPF, but in silverlight no.

This is code which fails under Silverlight:
Mock.Create<SomeClass<Period, Container>.SomeSubClass>(Telerik.JustMock.Behavior.CallOriginal);

I'm using JusMock 2012.3.1016.3.
Kaloyan
Telerik team
 answered on 31 May 2013
3 answers
115 views
Hi, how do I mock the examble below?

I want o mock the Datetime method that is called inside the class.

Also, how to mock mscorlib methods for the whole class and not for a particular method?

Thanks,
Gustavo
public static class MyStaticClass
{
     public static string MyStaticMethod()
     {
          return Datetime.Now.ToString() + "anotherRandomString";
     }
}
Kaloyan
Telerik team
 answered on 29 May 2013
3 answers
123 views
Hi All,
I hope you can help me with this. I am using a full version of JustMock and am attempting to mock a large class with multiple dependencies. Automocking seemed to be the way to go for this (unit) test. So I referenced the Telerik.JustMock.Container dll in my project, added the proper using statement and wrote the following simple test:

public void TestAutoMock()
{
// Arrange

var autoMockMFContainer = new MockingContainer<MyCustomClass>();

autoMockMFContainer.Instance.EstablishMyEmployee(10000, 2, 100007, 2, true, false);
}

This seems simple enough but it fails with a NullReferenceException. "Instance" is null. I am new to JustMock so I suspect the problem may lie with the project references or some configuration item but I don't know what it is. 

Can anyone offer any suggestions as to how I might get the Instance to not be null?

Thanks
MM
Kaloyan
Telerik team
 answered on 29 May 2013
3 answers
277 views
Hi,

I have a unit test where I assert that a certain action will call a method on a mock dependency object by using Mock.Assert().  I want to ensure that the action I take calls the method on the mock object exactly once.  The problem is that the setup of the unit test creates a scenario where the method of the mock object will also be called, so when I assert that the call to the mock object happened just once it fails because it has actually been called more than once.

Is there a way to "reset" the call tracking of methods on mock objects?  I basically want to tell JustMock that at a certain point, whatever calls have happened to my mock objects should be discarded and the call counter should basically start at 0 again.

Am I missing something in the framework or is there another way I should do this?

Jerrie
Kaloyan
Telerik team
 answered on 29 May 2013
3 answers
147 views
HI,

I tried to create mock of difficult custom type, but I always have an exception like
System.TypeLoadException: System.TypeLoadException: Method 'UnloadRecursive' on type 'BaseProxy+...' from assembly 'Telerik.JustMock.Dynamic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is overriding a method that is not visible from that assembly..

I tried to use attribute InternalsVisibleTo like <Assembly: InternalsVisibleTo("Telerik.JustMock.Dynamic")> but it didn't solve the problem. I tried to create mock of parent types, all fine. I tried to use MockingContainer, but this no matter, because our type haven't external dependencies. I can't to find where the problem.

What you can to suggest me?


Thanks
Kaloyan
Telerik team
 answered on 28 May 2013
12 answers
531 views
Hi,

I'm currently using the trial of JustMock to write some demo tests for our code. We are using Sitecore CMS and it uses a lot of static properties.

I have this very simple class I want to use for some testing and I have mocked the static property Item on the Context like so:

public class when_accessing_the_context_item_it_should_return_the_name
    {
        static string _result;

        Establish context = () =>
            {
                Mock.SetupStatic(typeof(Sitecore.Context), StaticConstructor.Mocked);

                var mockItem = Mock.Create<Item>();
                Mock.Arrange(() => mockItem.Name).Returns("thename");
                Mock.Arrange(() => Sitecore.Context.Item).Returns(mockItem);
            };

        Because of = () =>
        {
            _result = new MyRendering().Render();
        };

        It should_return_the_name_of_the_item = () =>
        {
            _result.ShouldEqual("thename");
        };


    }

    public class MyRendering
    {
        public string Render()
        {
            return Sitecore.Context.Item.Name;
        }
    }

However when running the test I get an exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: itemID
   at Telerik.JustMock.DynamicProxy.ProxyFactory.CreateInstance(Type type, Object[] extArgs)
   at Telerik.JustMock.DynamicProxy.ProxyFactory.Create()
   at Telerik.JustMock.DynamicProxy.Fluent.FluentProxy.NewInstance()
   at Telerik.JustMock.DynamicProxy.Proxy.Create(Type type, Action`1 action)
   at Telerik.JustMock.MockManager.CreateProxy(Type targetType, Container container)
   at Telerik.JustMock.MockManager.CreateMock(Container& container)
   at Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean isStaticType)
   at Telerik.JustMock.MockManager.CreateInstance()
   at Telerik.JustMock.Mock.Create(Type target, Behavior behavior, Object[] args)
   at Telerik.JustMock.Mock.Create(Type target, Object[] args)
   at Telerik.JustMock.Mock.Create[T]()
   at Demo.Tests.When_accessing_the_context_item_it_should_return_the_name.<.ctor>b__0() in 

Why am I seeing this exception, am I doing something wrong?
Kaloyan
Telerik team
 answered on 28 May 2013
1 answer
90 views
Hi I would like to test this piece of code:

/// <summary>
    /// Represents the Automatic Warehouse add in
    /// </summary>
    ///  /// <seealso cref=""/>
    public class AutomaticWarehouseKpiAddInn: IKeyPerformanceIndicatorAddIn
    {
  
        /// <summary>
        /// Gets or sets the configuration extension service.
        /// </summary>
        [Dependency]
        public IConfigurationExtensionService ConfigurationExtensionService { get; set; }
  
        public void Initialize()
        {
            ConfigurationExtensionService.RegisterExtension(typeof(AutomaticWarehouseCommanderAddInConfiguration),
                                                             new AutomaticWarehouseCommanderConveyorConfigConverter());
        }
    }

And I tried to test it like this:

  
[TestMethod]
      public void InitializeRegisterExtensionCorrectly()
      {
          IConfigurationExtensionService configurationExtensionService = Mock.Create<ConverterConfigurationBuilder>();
 
          AutomaticWarehouseKpiAddInn automaticWarehouseKpiAddInn = new AutomaticWarehouseKpiAddInn
              {
                  ConfigurationExtensionService = configurationExtensionService
              };
          Mock.Arrange(() => configurationExtensionService.RegisterExtension(null, null)).MustBeCalled();
          automaticWarehouseKpiAddInn.Initialize();
 
          Mock.Assert(configurationExtensionService);
      }


What I am doing wrong?
Kaloyan
Telerik team
 answered on 23 May 2013
1 answer
90 views
Hi,

I tried to do some MsCorLib Mocking following the Datetime.Now example (http://www.telerik.com/help/justmock/advanced-usage-mscorlib-mocking.html) and adjust it for Stopwatch.GetTimestamp().

But somehow it does not work - What do I miss?
Any help or hints would be great

Tobias

[TestClass]
public class MsCorlibFixture
{
    static MsCorlibFixture()
    {
        Mock.Replace(() => Stopwatch.GetTimestamp()).In<MsCorlibFixture>(x => x.ShouldAssertCustomValueForDateTime());
    }
 
    [TestMethod]
    public void ShouldAssertCustomValueForDateTime()
    {
        Mock.Arrange(() => Stopwatch.GetTimestamp()).Returns(100);
 
        var now = Stopwatch.GetTimestamp()
 
        Assert.AreEqual(100, now);
    }
}
Kaloyan
Telerik team
 answered on 14 May 2013
7 answers
127 views
Is it possible to integrate JustMock with OpenCover?
I found only information with PartCover but I couldn't find similiar file in OpenCover. 
Kaloyan
Telerik team
 answered on 10 May 2013
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?