public class UnmockableClass<T> { private bool _dummyvariable; public UnmockableClass() { _dummyvariable = true; } public static UnmockableClass<T> GetInstance<TDescriptor>() where TDescriptor : IDescriptor<T> { throw new NotImplementedException(); } }[TestClass] public class UnmockableClassTest { [TestMethod] public void SimpleTest() { Mock.SetupStatic<UnmockableClass<AvailableID>>(); //nothing else. The test will fail with just this line of code above } }Test 'JustMockIssueTestMock.UnmockableClassTest.SimpleTest' failed: Test method JustMockIssueTestMock.UnmockableClassTest.SimpleTest threw exception: System.ArgumentException: GenericArguments[0], 'JustMockIssue.IDescriptor`1[T]', on 'JustMockIssue.UnmockableClass`1[JustMockIssue.AvailableID] GetInstance[TDescriptor]()' violates the constraint of type 'TDescriptor'. ---> System.Security.VerificationException: Method JustMockIssue.UnmockableClass`1[JustMockIssue.AvailableID].GetInstance: type argument 'JustMockIssue.IDescriptor`1[T]' violates the constraint of type parameter 'TDescriptor'. at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation) at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation) --- End of inner exception stack trace --- at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation) at Telerik.JustMock.Weaver.WeaverInterceptorBuilder.DefineInterceptor(MethodBase methodBase, FieldBuilder fldInterceptor, Type[] parameters) at Telerik.JustMock.Weaver.DynamicInjector.EmitInterceptor(ILGenerator il, MethodBase methodInfo) at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase, MethodInfo containerMethodInfo) at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase, MethodInfo injectingMethod, Boolean force) at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase) at Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean static) at Telerik.JustMock.MockManager.SetupStatic() at Telerik.JustMock.Mock.SetupStatic(Type targetType, Behavior behavior, StaticConstructor staticConstructor) at Telerik.JustMock.Mock.SetupStatic[T]() UnmockableClassTest.cs(17,0): at JustMockIssueTestMock.UnmockableClassTest.SimpleTest()[Test]public void datetime_test_justmock(){ var YEAR_2K = new DateTime(2000, 1, 1); Mock.Arrange(() => DateTime.Now).Returns(YEAR_2K); var actual = DateTime.Now; Assert.AreEqual(YEAR_2K, actual);}var field = Mock.Create<SPField>();at Telerik.JustMock.DynamicProxy.ProxyFactory.CreateInstance(Type proxyType, Object[] extArgs)at Telerik.JustMock.DynamicProxy.ProxyFactory.Create()at Telerik.JustMock.DynamicProxy.Fluent.FluentProxy.NewInstance()at Telerik.JustMock.DynamicProxy.Proxy.Create(Type target, Action`1 action)at Telerik.JustMock.MockManager.CreateProxy(Type targetType, Container container)at Telerik.JustMock.MockManager.CreateInstance(Container container)at Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean static)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]()Hello,
this question has been ask here before, but I do not see how the answer is of any use in this case.
I want to test a method where I have something like this:
public string DoSomething() { using (SPSite site = new SPSite("http://someURL")) { using (SPWeb web = site.OpenWeb()) {…
At this time the URL does not exist.
In the test case I create a mock for SPSite and SPWeb (SharePoint objects).
var spSite = Mock.Create<SPSite>(); var spWeb = Mock.Create<SPWeb>();Mock.Arrange(() => spSite.OpenWeb()).Returns(spWeb);…System.IO.FileNotFoundException: The Web application at http://someUrl could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
How can I use the mock spSite when new SPSite("http://someURL")) is called?
IgnoreInstance only swaps method calls and does not work with constructor calls. (?)
[TestMethod]public void TestMethod1(){ // Arrange var appSettings = new NameValueCollection { { "test1", "one" } }; Mock.Arrange(() => ConfigurationManager.AppSettings) .Returns(appSettings) .MustBeCalled(); // Act var test1 = ConfigurationManager.AppSettings["test1"]; // Assert Assert.IsTrue(Mock.IsProfilerEnabled); Assert.AreEqual("one", test1);}public interface IClientServerMockingTest{ void CallMethod(string id, Action<string> status);}[TestClass]public class ClientServerMockingTest : IClientServerMockingTest{ [TestMethod] public void TestMethod1() { var clientServer = Mock.Create<ClientServerMockingTest>(); var action = Mock.Create<Action<string>>(); Mock.Arrange(() => clientServer.CallMethod("demo", action)).Raises(() => clientServer.CallMethod("demo", action)); clientServer.CallMethod("demo", action); Mock.Assert(clientServer); } public void CallMethod(string id, Action<string> action) { action.Invoke("worked"); }}public Box CreateBoxFromDataSet(DataSet dataSet, int rowNumber = 0){ const int BOX_TABLE = 0; const int METADATA_TABLE = 1; Box box = new Box() { ID = Convert.ToInt64(dataSet.Tables[BOX_TABLE].Rows[rowNumber]["BoxID"]), Name = dataSet.Tables[BOX_TABLE].Rows[rowNumber]["Name"].ToString() }; if (dataSet.Tables.Count > 1) { foreach(DataRow row in dataSet.Tables[METADATA_TABLE].Rows) { long propertyID = Convert.ToInt64(row["MetaPropertyID"]); string value = NullableConverter.ToString(row["MetadataValue"]); box.ApplyMetadata(propertyID, value); } } return box;}[TestMethod]public void CreateBoxFromDataSet_ReturnsBox(){ //Arrange long expectedID = 300; Box actual = null; var manager = new BoxManager(); var dataset = new DataSet(); dataset.Tables.AddRange(new [] { new DataTable("Box"), new DataTable("Metadata") }); dataset.Tables[0].Rows.Add(dataset.Tables[0].NewRow()); dataset.Tables[1].Rows.Add(dataset.Tables[1].NewRow()); dataset.Tables[2].Rows.Add(dataset.Tables[2].NewRow()); Mock.Arrange(() => dataset.Tables[0].Rows[0]["BoxID"]).Returns(expectedID); Mock.Arrange(() => dataset.Tables[0].Rows[0]["Name"]).Returns("BoxName"); Mock.Arrange(() => dataset.Tables[1].Rows[0]["MetaPropertyID"]).Returns(700); Mock.Arrange(() => dataset.Tables[1].Rows[0]["MetadataValue"]).Returns("MetadataValue"); //Act actual = manager.CreateBoxFromDataSet(dataset); //Assert Assert.IsNotNull(actual);}I'm looking to see what is wrong with this code. Looking at the samples it should work with the Telerik.JustMock.DLL referenced, but is throwing a build error instead:
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Telerik.JustMock;
namespace JustMock_Example
{
public class ClassUnderTest
{
public bool ReturnsFalse()
{
return false;
}
}
[TestClass]
public class IgnoreInstanceTest
{
[TestMethod]
public void ExampleTest()
{
var fake = Mock.Create<ClassUnderTest>();
Mock.Arrange(() => fake.ReturnsFalse()).Returns(true).IgnoreInstance();
Assert.IsTrue(new ClassUnderTest().ReturnsFalse());
}
}
}
///Throws the following:
Error 1 'Telerik.JustMock.Expectations.Abstraction.IAssertable' does not contain a definition for 'IgnoreInstance' and no extension method 'IgnoreInstance' accepting a first argument of type 'Telerik.JustMock.Expectations.Abstraction.IAssertable' could be found (are you missing a using directive or an assembly reference?) c:\~\~\documents\visual studio 2010\Projects\JustMock Example\Class1.cs 23 67 JustMock Example
This is with the most recent build, and hopefully I'm missing something small.
Thanks!