Run JustMockLite automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
/*
JustMock Lite
Copyright © 2010-2015 Telerik EAD
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System.Collections.Generic;
using Telerik.JustMock.Tests.EventFixureDependencies;
using NUnit.Framework;
namespace Telerik.JustMock.Tests
{
[TestFixture]
public class EventsFixtureNUnit
{
private ProjectNavigatorViewModel viewModel;
private ISolutionService solutionService;
[SetUp]
public void Initialize()
{
this.solutionService = Mock.Create<ISolutionService>();
this.viewModel = new ProjectNavigatorViewModel(this.solutionService);
}
[Test, Category("Lite"), Category("Events")]
[TestCaseSource("DummyTestCaseSource")]
public void ShouldRaiseEventsOnDataDrivenTests(object _)
{
Mock.Raise(() => this.solutionService.ProjectAdded += null, new ProjectEventArgs(null));
}
private static IEnumerable<TestCaseData> DummyTestCaseSource = new[] { new TestCaseData(null), new TestCaseData(null) };
}
}
/**
* Copyright 2015 Marc Rufer, d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Telerik.JustMock;
using Telerik.JustMock.Core;
namespace biz.dfch.CS.JustMock.Examples.Tests
{
[TestClass]
public class ArgumentMatchingTests
{
private Boolean _called;
[TestInitialize]
public void TestInitialize()
{
_called = false;
}
/**
* For more details consult the Wiki
* https://github.com/dfensgmbh/biz.dfch.CS.JustMock.Examples/wiki/ArgumentMatchingTests#matching-specific-argument
**/
[TestMethod]
public void MockedConsoleWriteLineGettingCalledWithMatchingArgument()
{
// Arrange
Mock.SetupStatic(typeof(Console), StaticConstructor.Mocked);
Mock.Arrange(() => Console.WriteLine("Test"))
.DoInstead(() => { _called = true; })
.OccursOnce();
// Act
Console.WriteLine("Test");
// Assert
Assert.IsTrue(_called);
Mock.Assert(() => Console.WriteLine("Test"));
}
[TestMethod]
public void MockedConsoleWriteLineNotGettingCalledWithSpecificArgument()
{
// Arrange
Mock.SetupStatic(typeof(Console), StaticConstructor.Mocked);
Mock.Arrange(() => Console.WriteLine("Test"))
.DoInstead(() => { _called = true; })
.OccursNever();
// Act
Console.WriteLine("Something");
// Assert
Assert.IsFalse(_called);
Mock.Assert(() => Console.WriteLine("Test"));
}
/**
* For more details consult the Wiki
* https://github.com/dfensgmbh/biz.dfch.CS.JustMock.Examples/wiki/ArgumentMatchingTests#matching-any-argument-of-type
**/
[TestMethod]
public void MockedCDummyGettingCalledWithArgumentOfSpecificType()
{
// Arrange
var mock = Mock.Create<MockSamplesHelper>();
Mock.Arrange(() => mock.Dummy(Arg.AnyString))
.OccursOnce();
// Act
mock.Dummy("Something");
// Assert
Mock.Assert(mock);
}
[TestMethod]
public void MockedDummyGettingCalledWithNullAsArgument()
{
// Arrange
var mock = Mock.Create<MockSamplesHelper>();
Mock.Arrange(() => mock.Dummy(Arg.AnyString))
.OccursOnce();
// Act
mock.Dummy(null);
// Assert
Mock.Assert(mock);
}
[TestMethod]
public void MockedDummyGettingCalledWithNullAsArgument2()
{
// Arrange
var mock = Mock.Create<MockSamplesHelper>();
Mock.Arrange(() => mock.Dummy(Arg.IsAny<String>()))
.OccursOnce();
// Act
mock.Dummy(null);
// Assert
Mock.Assert(mock);
}
[TestMethod]
public void MockedDummySetsCalled()
{
//Arrange
MockSamplesHelper mock = new MockSamplesHelper();
Mock.Arrange(() => mock.Dummy(""))
.DoInstead(() => _called = true)
.OccursOnce();
//Act
mock.Dummy("");
//Assert
Assert.IsTrue(_called);
Mock.Assert(mock);
}
[TestMethod]
public void MockedAddReturns4()
{
//Arrange
int result = 0;
MockSamplesHelper mock = new MockSamplesHelper();
Mock.Arrange(() => mock.Add(Arg.IsAny<int>(), Arg.IsAny<int>()))
.DoInstead(() => result = 4)
.OccursOnce();
//Act
mock.Add(3, 3);
//Assert
Assert.AreEqual(result, 4);
Mock.Assert(mock);
}
[TestMethod]
[ExpectedException(typeof (ArgumentException))]
public void MockedAddThrowsArgumentException()
{
//Arrange
MockSamplesHelper mock = new MockSamplesHelper();
Mock.Arrange(() => mock.Add(Arg.IsAny<int>(), Arg.IsAny<int>()))
.Throws(new ArgumentException())
.OccursOnce();
//Act
mock.Add(2, 2);
// Assert
Mock.Assert(mock);
}
[TestMethod]
public void MockedAddArgumentsAreInRangeFrom20To30()
{
//Arrange
MockSamplesHelper mock = new MockSamplesHelper();
Mock.Arrange(
() => mock.Add(Arg.IsInRange(20, 30, RangeKind.Inclusive), Arg.IsInRange(20, 30, RangeKind.Inclusive)))
.Returns(2)
.OccursOnce();
//Act
var result = mock.Add(29, 27);
//Assert
Assert.AreEqual(2,result);
Mock.Assert(mock);
}
[TestMethod]
public void MockedDummyGetsCalledFromAdd()
{
//Arrange
MockSamplesHelper mock = new MockSamplesHelper();
Mock.Arrange(() => mock.Dummy(Arg.AnyString))
.OccursOnce();
//Act
mock.Add(4, 3);
//Assert
Mock.Assert(mock);
}
[TestMethod]
public void MockedAddReturns7IfCalledWithArguments1And1()
{
//Arrange
MockSamplesHelper mock = new MockSamplesHelper();
Mock.Arrange(() => mock.Add(1, 1))
.Returns(7)
.OccursOnce();
//Act
var result = mock.Add(1, 1);
//Assert
Assert.AreEqual(7,result);
Mock.Assert(mock);
}
[TestMethod]
[ExpectedException(typeof (StrictMockException))]
public void MockedSomePropertyThrowsWhenSetToSomewhatNotEqualTheMock()
{
//Arrange
var mock = Mock.Create<MockSamplesHelper>(Behavior.Strict);
Mock.ArrangeSet(() => mock.SomeProperty = false);
//Act
mock.SomeProperty = true;
//Assert
Mock.Assert(mock);
}
}
}
/**
* Copyright 2015 Marc Rufer, d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.JustMock;
namespace biz.dfch.CS.JustMock.Examples.Tests
{
[TestClass]
public class BasicStaticMockingTests
{
/**
* For more details consult the Wiki
* https://github.com/dfensgmbh/biz.dfch.CS.JustMock.Examples/wiki/BasicStaticMockingTests#mocking-datetimeoffsetutcnow
**/
[TestMethod]
public void MockedDateTimeOffsetReturnsExpectedDateTimeOffset()
{
// Arrange
Mock.SetupStatic(typeof(DateTimeOffset), Behavior.CallOriginal, StaticConstructor.Mocked);
Mock.Arrange(() => DateTimeOffset.UtcNow)
.Returns(new DateTimeOffset(2015, 1, 1, 3, 10, 7, new TimeSpan()))
.MustBeCalled();
// Act & Assert
Assert.AreEqual(new DateTimeOffset(2015, 1, 1, 3, 10, 7, new TimeSpan()), DateTimeOffset.UtcNow);
// Assert Occurence
Mock.Assert(() => DateTimeOffset.UtcNow);
}
[TestMethod]
public void MockedConsoleShouldReturn9()
{
//Arrange
Mock.SetupStatic(typeof(Dummy),Behavior.Strict);
Mock.Arrange(() => Dummy.Get7())
.Returns(9)
.OccursOnce();
//Act
int mockedResult = Dummy.Get7();
//Assert
Assert.AreEqual(9,mockedResult);
Mock.Assert(typeof(Dummy));
}
[TestMethod]
public void MockedConsoleWillSetCalledOnTrue()
{
//Arrange
Mock.SetupStatic(typeof(Console),Behavior.Strict);
bool _called = false;
Mock.Arrange(() => Console.Clear())
.DoInstead(() =>{ _called = true;})
.OccursOnce();
//Act
Console.Clear();
//Assert
Assert.AreEqual(true,_called);
Mock.Assert(typeof (Console));
}
}
}
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.