Here are the examples of the csharp api FakeItEasy.A.CallTo(object) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
20 Examples
19
View Source File : AnyCallConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void WithNonVoidReturnType(IFoo fake)
{
"Given a fake with a generic method"
.x(() => fake = A.Fake<IFoo>());
"When the fake's generic method is configured to return null for any non-void return type"
.x(() => A.CallTo(fake).Where(call => call.Method.Name == "Bar").WithNonVoidReturnType().Returns(null));
"Then the configured method returns null when called with generic argument String"
.x(() => fake.Bar<string>().Should().BeNull());
"And the configured method returns null when called with generic argument IFoo"
.x(() => fake.Bar<IFoo>().Should().BeNull());
}
19
View Source File : AnyCallConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
[System.Diagnostics.Codereplacedysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = nameof(IFoo), Justification = "It's an identifier")]
public static void WithReturnType(
IFoo fake,
string returnValue)
{
"Given a fake with a generic method"
.x(() => fake = A.Fake<IFoo>());
"When the fake's generic method is configured to return a specific value for a given return type"
.x(() => A.CallTo(fake).Where(call => call.Method.Name == "Bar").WithReturnType<string>().Returns(returnValue = "hello world"));
"Then the configured method returns the specified value when called with generic argument String"
.x(() => fake.Bar<string>().Should().Be(returnValue));
"And the configured method returns a dummy when called with generic argument IFoo"
.x(() => fake.Bar<IFoo>().Should().NotBeNull().And.BereplacedignableTo<IFoo>());
}
19
View Source File : AnyCallConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void WithReturnTypeDescription(
IFoo fake,
Exception exception)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());
"When an replacedertion is made that a method with a particular return type was called"
.x(() => exception = Record.Exception(() => A.CallTo(fake).WithReturnType<Guid>().MustHaveHappened()));
"Then an exception is thrown"
.x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()
.WithMessage("*Any call with return type System.Guid to the fake object.*"));
}
19
View Source File : AnyCallConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void AnyCallDescription(
IFoo fake,
Exception exception)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());
"When an replacedertion is made that any method was called"
.x(() => exception = Record.Exception(() => A.CallTo(fake).MustHaveHappened()));
"Then an exception is thrown"
.x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()
.WithMessage("*Any call made to the fake object.*"));
}
19
View Source File : ConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void CallToObjectOnNonFake(
BaseClreplaced notAFake,
Exception exception)
{
"Given an object that is not a fake"
.x(() => notAFake = new BaseClreplaced());
"When I start to configure the object"
.x(() => exception = Record.Exception(() => A.CallTo(notAFake)));
"Then it throws an argument exception"
.x(() => exception.Should().BeAnExceptionOfType<ArgumentException>()
.And.Message.Should().Contain("Object 'FakeItEasy.Specs.ConfigurationSpecs+BaseClreplaced' of type 'FakeItEasy.Specs.ConfigurationSpecs+BaseClreplaced' is not recognized as a fake object."));
}
19
View Source File : ConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void DoesNothingAfterStrictValueTypeKeepsDefaultReturnValue(
IFoo fake,
int result)
{
"Given a strict fake"
.x(() => fake = A.Fake<IFoo>(options => options.Strict()));
"And I configure all methods to do nothing"
.x(() => A.CallTo(fake).DoesNothing());
"When I call a value type method"
.x(() => result = fake.Baz());
"Then it returns the same value as an unconfigured fake"
.x(() => result.Should().Be(A.Fake<IFoo>().Baz()));
}
19
View Source File : ConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void DoesNothingAfterStrictNonFakeableReferenceTypeKeepsDefaultReturnValue(
IFoo fake,
string result)
{
"Given a strict fake"
.x(() => fake = A.Fake<IFoo>(options => options.Strict()));
"And I configure all methods to do nothing"
.x(() => A.CallTo(fake).DoesNothing());
"When I call a non-fakeable reference type method"
.x(() => result = fake.Bas());
"Then it returns the same value as an unconfigured fake"
.x(() => result.Should().Be(A.Fake<IFoo>().Bas()));
}
19
View Source File : ConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void InvokesAfterStrictNonFakeableReferenceTypeKeepsDefaultReturnValue(
IFoo fake,
string result)
{
"Given a strict fake"
.x(() => fake = A.Fake<IFoo>(options => options.Strict()));
"And I configure all methods to invoke an action"
.x(() => A.CallTo(fake).Invokes(() => { }));
"When I call a non-fakeable reference type method"
.x(() => result = fake.Bas());
"Then it returns the same value as an unconfigured fake"
.x(() => result.Should().Be(A.Fake<IFoo>().Bas()));
}
19
View Source File : AssertionsTests.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Fact]
public void Should_not_throw_when_replacederting_while_calls_are_being_made_on_the_fake()
{
var fake = A.Fake<ISomething>();
fake.SomethingMethod();
A.CallTo(fake)
.Where(
call =>
{
// Simulate a concurrent call being made in another thread while the `MustHaveHappened` replacedertion
// is being evaluated. This method is unorthodox, but is deterministic, not relying on the
// vagaries of the thread scheduler to ensure that the calls overlap.
fake.SomethingMethod();
return true;
},
output => { })
.MustHaveHappened();
}
19
View Source File : AnyCallConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void WithNonVoidReturnTypeAndVoidMethod(
IFoo fake,
bool methodWasIntercepted)
{
"Given a fake with a void method"
.x(() => fake = A.Fake<IFoo>());
"And the fake is configured to set a flag for any non-void return type"
.x(() => A.CallTo(fake).WithNonVoidReturnType().Invokes(() => methodWasIntercepted = true));
"When the void method is called"
.x(() => fake.Baz());
"Then the flag is not set"
.x(() => methodWasIntercepted.Should().BeFalse());
}
19
View Source File : AnyCallConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void WithNonVoidReturnTypeDescription(
IFoo fake,
Exception exception)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());
"When an replacedertion is made that a non-void method was called"
.x(() => exception = Record.Exception(() => A.CallTo(fake).WithNonVoidReturnType().MustHaveHappened()));
"Then an exception is thrown"
.x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()
.WithMessage("*Any call with non-void return type to the fake object.*"));
}
19
View Source File : ConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void DoesNothingAfterStrictFakeableReferenceTypeKeepsDefaultReturnValue(
IFoo fake,
IFoo result)
{
"Given a strict fake"
.x(() => fake = A.Fake<IFoo>(options => options.Strict()));
"And I configure all methods to do nothing"
.x(() => A.CallTo(fake).DoesNothing());
"When I call a fakeable reference type method"
.x(() => result = fake.Bafoo());
"Then it returns the same value as an unconfigured fake"
.x(() => result.Should().Be(A.Fake<IFoo>().Bafoo()));
}
19
View Source File : ConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void InvokesAfterStrictFakeableableReferenceTypeKeepsDefaultReturnValue(
IFoo fake,
IFoo result)
{
"Given a strict fake"
.x(() => fake = A.Fake<IFoo>(options => options.Strict()));
"And I configure all methods to invoke an action"
.x(() => A.CallTo(fake).Invokes(() => { }));
"When I call a fakeable reference type method"
.x(() => result = fake.Bafoo());
"Then it returns the same value as an unconfigured fake"
.x(() => result.Should().Be(A.Fake<IFoo>().Bafoo()));
}
19
View Source File : ConfigurationSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void replacedignsOutAndRefParametersForAllMethodsKeepsDefaultReturnValue(
IFoo fake,
IFoo result,
int i)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());
"And I configure all methods to replacedign out and ref parameters"
.x(() => A.CallTo(fake).replacedignsOutAndRefParameters(0));
"When I call a reference type method"
.x(() => result = fake.Bafoo(out i));
"Then it returns the same value as an unconfigured fake"
.x(() => result.Should().Be(A.Fake<IFoo>().Bafoo(out i)));
}
19
View Source File : AssertingOnSetOnlyPropertiesSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void SetOnlyProperties(
ISetOnly setOnly)
{
"establish"
.x(() => setOnly = A.Fake<ISetOnly>());
"when replacedertion on set only properties"
.x(() =>
{
setOnly.MyProperty = 1;
setOnly.MyProperty2 = false;
});
"it should be able to replacedert with argument constraint"
.x(() => A.CallTo(setOnly).Where(x => x.Method.Name == "set_MyProperty")
.WhenArgumentsMatch(x => x.Get<int>(0) == 1).MustHaveHappened());
}
19
View Source File : ThenSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void AnyCallWithInvokesAndThrows(
IFoo foo,
Action action1,
Action action2,
Exception exception)
{
"Given a fake"
.x(() => foo = A.Fake<IFoo>());
"And two delegates"
.x(() =>
{
action1 = A.Fake<Action>();
action2 = A.Fake<Action>();
});
"When any method is configured to invoke a delegate once, then another delegate twice, then throw an exception"
.x(() =>
A.CallTo(foo).Invokes(action1).DoesNothing().Once()
.Then.Invokes(action2).DoesNothing().Twice()
.Then.Throws<InvalidOperationException>());
"And 4 calls are made on the fake"
.x(() =>
{
foo.Bar();
foo.Bar(1);
foo.Bar(2);
exception = Record.Exception(() => foo.Bar(3));
});
"Then the first delegate is invoked once, and the second delegate is invoked twice"
.x(() => A.CallTo(() => action1()).MustHaveHappened(Repeated.Exactly.Once)
.Then(A.CallTo(() => action2()).MustHaveHappened(Repeated.Exactly.Twice)));
"And the fourth call throws an exception"
.x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>());
}
19
View Source File : SelfInitializedFakesSpecs.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Scenario]
public static void SelfInitializing(
InMemoryStorage inMemoryStorage,
ILibraryService realServiceWhileRecording,
ILibraryService realServiceDuringPlayback,
IEnumerable<int> countsWhileRecording,
IEnumerable<int> countsDuringPlayback)
{
"Given a call storage object"
.x(() => inMemoryStorage = new InMemoryStorage());
"And a real service to wrap while recording"
.x(() =>
{
realServiceWhileRecording = A.Fake<ILibraryService>();
A.CallTo(() => realServiceWhileRecording.GetCount("1"))
.ReturnsNextFromSequence(0x1A, 0x1B);
A.CallTo(() => realServiceWhileRecording.GetCount("2"))
.Returns(0x2);
});
"And a real service to wrap while playing back"
.x(() => realServiceDuringPlayback = A.Fake<ILibraryService>());
"When I use a self-initialized fake in recording mode to get the counts for book 1, 2, and 1 again"
.x(() =>
{
using (var recorder = new RecordingManager(inMemoryStorage))
{
var fakeService = A.Fake<ILibraryService>(options => options
.Wrapping(realServiceWhileRecording).RecordedBy(recorder));
countsWhileRecording = new List<int>
{
fakeService.GetCount("1"),
fakeService.GetCount("2"),
fakeService.GetCount("1")
};
}
});
"And I use a self-initialized fake in playback mode to get the counts for book 1, 2, and 1 again"
.x(() =>
{
using (var recorder = new RecordingManager(inMemoryStorage))
{
var playbackFakeService = A.Fake<ILibraryService>(options => options
.Wrapping(realServiceDuringPlayback).RecordedBy(recorder));
countsDuringPlayback = new List<int>
{
playbackFakeService.GetCount("1"),
playbackFakeService.GetCount("2"),
playbackFakeService.GetCount("1")
};
}
});
"Then the recording fake forwards calls to the wrapped service"
.x(() => A.CallTo(() => realServiceWhileRecording.GetCount("1"))
.MustHaveHappened(Repeated.Exactly.Twice));
"And the recording fake returns the wrapped service's results"
.x(() => countsWhileRecording.Should().Equal(0x1A, 0x2, 0x1B));
"And the playback fake does not forward calls to the wrapped service"
.x(() => A.CallTo(realServiceDuringPlayback).MustNotHaveHappened());
"And the playback fake returns the recorded results"
.x(() => countsDuringPlayback.Should().Equal(0x1A, 0x2, 0x1B));
}
19
View Source File : DefaultSutInitializerTests.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Fact]
public void Should_preplaced_empty_options_builder_to_fake_manager_for_each_constructor_argument()
{
// Arrange
var optionsBuilders = new List<Action<IFakeOptions>>();
A.CallTo(() => this.fakeManager.CreateFake(A<Type>._, A<Action<IFakeOptions>>._))
.Invokes((Type type, Action<IFakeOptions> optionBuilder) => optionsBuilders.Add(optionBuilder))
.ReturnsLazily((Type type, Action<IFakeOptions> fakeOptionBuilder) => Create.Fake(type));
// Act
this.sutInitializer.CreateSut(typeof(TypeWithFakeableDependencies), (x, y) => { });
// replacedert
optionsBuilders.Should().HaveCount(2, "because all constructor arguments should be configured");
var fakeOptions = A.Fake<IFakeOptions>();
foreach (var optionsBuilder in optionsBuilders)
{
optionsBuilder.Invoke(fakeOptions);
}
A.CallTo(fakeOptions).MustNotHaveHappened();
}
19
View Source File : DynamicOptionsBuilderTests.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Fact]
public void BuildOptions_should_do_nothing_when_fake_type_has_no_options_builder_specified()
{
// Arrange
var optionsBuilder = this.CreateOptionsBuilder();
var fakeOptions = A.Fake<IFakeOptions>();
// Act
optionsBuilder.BuildOptions(typeof(TypeWithOptionsBuilders), fakeOptions);
// replacedert
A.CallTo(fakeOptions).MustNotHaveHappened();
}
19
View Source File : WhereConfigurationExtensionsTests.cs
License : MIT License
Project Creator : DynamicsValue
License : MIT License
Project Creator : DynamicsValue
[Fact]
public void Where_should_return_configuration_from_configuration()
{
// Arrange
var configurationToReturn = A.Dummy<IVoidConfiguration>();
var configuration = A.Fake<IWhereConfiguration<IVoidConfiguration>>();
A.CallTo(configuration).WithReturnType<IVoidConfiguration>().Returns(configurationToReturn);
// Act
var returnedConfiguration = configuration.Where(x => true);
// replacedert
returnedConfiguration.Should().BeSameAs(returnedConfiguration);
}