Telerik Forums
JustMock Forum
1 answer
119 views
Hey everyone,

I am trying to do some static mocking.  here is my test.
[Test]
       public void timesheet_processor_tests_just_mock()
       {
           Mock.SetupStatic(typeof(LaborManager));
           Mock.Arrange(() => LaborManager.GetTimesheetLabor(DateTime.Now, 1683)).Returns(new TimesheetLaborReadOnlyCollection());
           var processor = new TimesheetProcessor();
           processor.ProcessUserTimesheet(DateTime.Now, 1683);
           Mock.Assert(() => LaborManager.GetTimesheetLabor(DateTime.Now, 1683), Occurs.AtLeastOnce());
       }

The class TimesheetProcessor uses LaborManager.GetTimesheetLabor() method which I have setup to return a new Collection which by Default would have a count == 0. 

However, when I step into this and get into the TimesheetProcessor class where LaborManager.GetTimeSheetLabor() method is called.  the variable laborEntries which calls the method I arranged in my test gets set to null instead of a new collection.  Please see the timesheet processor code below

//Timesheet Processor Class
        public void ProcessUserTimesheet(DateTime weekEndingDate, int userId)
        {
            TimesheetsCreated = new List<int>();
            TimesheetLaborReadOnlyCollection laborEntries = LaborManager.GetTimesheetLabor(weekEndingDate, userId);
            if(laborEntries.Count > 0)
                ProcessTimesheetLaborUser(userId, weekEndingDate, laborEntries);
        }

Why is this happening?
Have I setup my test incorrectly?

I have JustMock enabled.

Thanks,

-zd
Ricky
Telerik team
 answered on 21 Sep 2010
1 answer
269 views
Does anyone any ideas on how to mock a static property in a Vb.Net module?

I have attempted various solutions, all of which lead to JustMock throwing exceptions.

I am looking for something like:
Mock.ArrangeSet(Sub() VbModule.MyProperty = Arg.AnyString).DoNothing()





Any help would be appreciated.






Ricky
Telerik team
 answered on 17 Sep 2010
1 answer
340 views
I am attempting to mock Registry.GetValue with specific parameters using the following code:

Mock.Arrange(Function() Registry.GetValue("HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Legacy App\Options""SettingValue"String.Empty)).Returns("Test Result")

I get the following exception:

{"Object reference not set to an instance of an object."}
   at ..( parsed, Type argType, Int32 argIndex, Expression argExpression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicParser.cs:line 114
   at ..Œ(MethodCallExpression expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicParser.cs:line 94
   at ..ˆ(Expression expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\ExpressionVisitor.cs:line 61
   at ..() in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\DynamicParser.cs:line 34
   at ..(Object obj, Type mockTarget, MethodInfo method, Expression expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 31
   at ..(Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 23
   at Telerik.JustMock.Mock...€( x) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs:line 75
   at ..[TDelgate,TReturn](† instruction, Func2`2 function) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\MockContext.cs:line 226
   at Telerik.JustMock.Mock.Arrange[TResult](Expression`1 expression) in c:\B\Basilisk\Basilisk CI Build\Sources\CodeBase\Telerik.JustMock\Mock.cs:line 73
   at xxxx.vb

Is there something that I can do differently?

Thanks,
Keith
Ricky
Telerik team
 answered on 16 Sep 2010
5 answers
95 views
... let's start at the very beginning.

First, I want to apologise for a posting that it only loosely based on JustMock.

I have never written a unit test in my life and thought, given that I have access to JustMock, that I should give it a go and that's why I need help.

I'll explain.

I'm running VS2K8SP1 under Win7 Ultimate to develop Web Sites and Wep Applications.

I've done a bit of reading and have created a Test Project with a Test Method. My reading has led me to understand that I need to decorate my Test Method like this...
[TestMethod]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:50612/TestBed/Default3.aspx")]

Trying to run this test resulting in a fail with the message ...

The test or test run is configured to run in ASP.NET in IIS, but the current user (Tosh-Laptop\Stuart) is not in the Administrators group. Running tests in ASP.NET in IIS requires the user to be in the Administrators group.

The thing is, I am in the Administrators group. The output from the command net localgroup Administrators confirms this ...
C:\Users\Stuart>net localgroup Administrators
Alias name     Administrators
Comment        Administrators have complete and unrestricted access to the compu
ter/domain
 
Members
 
-------------------------------------------------------------------------------
Administrator
Stuart
The command completed successfully.

So, I guess I'm missing something. Is anyone here running tests under a similar environment? Is anyone here better versed in what's needed to get me running.

Any help, either here, or - if you feel the response too off-topic for posting here - direct to me at sejhemming@gmail.com would be much appreciated.

-- 
Stuart
Ricky
Telerik team
 answered on 07 Sep 2010
1 answer
72 views
This refers to both the installed and online version of the docs.

If you go to figure 6 of this page you will see that includes a reference to the method Mock.CreateInstance<>().

Now, I've really only just started playing with JM, but I think that is is wrong.

-- 
Stuart
Ricky
Telerik team
 answered on 06 Sep 2010
5 answers
142 views
The example, in the documentation, for mocking mscorlib types assumes that you have direct access to the object you want to mock from the unit test. However, I'm trying to mock a filesystem scenario and the code that I'm testing creates its own instance of the DirectoryInfo type that I want to mock.

Here's what my code looks like:

public class TypeBeingTested
    {
        public TypeBeingTested()
        {
            var folder = new DirectoryInfo("C:\\Program Files");
            var folders = folder.GetDirectories("Microsoft Visual Studio 8");
 
            if (folders.Length == 0)
                throw new DirectoryNotFoundException("Visual Studio 2005 is not installed.");
        }
    }

I want to test the scenario where the Visual Studio folder cannot be found on my disk. The way I thought I could do this, looks like this:

[TestInitialize]
       public void Mock_DirectoryInfo()
       {
           Mock.Partial<DirectoryInfo>().For<string>((folder, name) => folder.GetDirectories(name));
       }
 
       [TestMethod]
       [ExpectedException(typeof(DirectoryNotFoundException))]
       public void Missing_Program_Files_folder_throws_exception()
       {
           var folder = new DirectoryInfo("C:\\Program Files");
 
           Mock.Arrange(() => folder.GetDirectories("Microsoft Visual Studio 8")).Returns(new DirectoryInfo[] {});
 
           var instance = new TypeBeingTested();
 
           Assert.Fail("No exception was raised in TypeBeingTested constructor.");
 
       }

Obviously, that instance of "folder" in my test method isn't being used for anything other than the Arrange statement... I knew as I was writing that, that this wasn't going to work; but, I can't figure out how this test should be constructed.

Can anyone help?

Thanks!
Chris
Telerik team
 answered on 27 Aug 2010
1 answer
86 views
Is there support for setup/teardown methods in JustMock?
Ricky
Telerik team
 answered on 27 Aug 2010
1 answer
137 views

I’m getting the following error when trying to mock a static function in a silverlight unit test:

“There were some problems intercepting the mock call. Optionally, please make sure that you have turned on JustMock's profiler while mocking concrete members.

I don’t get the error in a full .NET unit test project, so, I wondered whether I was missing a setting or something?

My code from my silverlight unit test project is below. I get the error on the Mock.Arrange() line

Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Telerik.JustMock
 
<TestClass()> _
Public Class Tests
 
    <TestMethod()> _
    Public Sub SharedMethodMustBeCalled()
 
        Mock.Arrange(Function() SharedStuff.GetSomething(Arg.AnyString)).MustBeCalled()
 
        Dim SomeClass As New SomeClass
        Dim ret As String = SomeClass.DoSomething()
 
        Mock.Assert(SomeClass)
    End Sub
 
    Public Class SomeClass
        Public Function DoSomething() As String
            Dim ret As String = SharedStuff.GetSomething("fred")
            Return ret
        End Function
    End Class
 
    Public Class SharedStuff
        Public Shared Function GetSomething(ByVal Name As String) As String
            Return "test"
        End Function
    End Class
 
End Class





Ricky
Telerik team
 answered on 24 Aug 2010
1 answer
118 views
Could you help me convert this Rhino Mock code to JustMock?

[Test]
public void Analyze_TooShortFileName_ErrorLoggedToService()
{
MockRepository mocks = new MockRepository();
IWebService simulatedService =
MockRespository.DynamicMock<IWebService>();
using(mocks.Record())
{
//we expected "Filename too short:abc.ext"
simulatedService.LogError("bad string");
}
LogAnalyzer log = new LogAnalyzer(simulatedService);
string tooShortFileName="abc.ext";
log.Analyze(tooShortFileName);
mocks.VerifyAll();
}
Ricky
Telerik team
 answered on 23 Aug 2010
5 answers
270 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
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?