fakeiteasy / fakeiteasy Goto Github PK
View Code? Open in Web Editor NEWThe easy mocking library for .NET
Home Page: https://fakeiteasy.github.io
License: MIT License
The easy mocking library for .NET
Home Page: https://fakeiteasy.github.io
License: MIT License
Big fan of framework, found a small annoyance:
I'd like to assert that a property setter was not called. I'm looking at your documentation, and I cant find a way to do this.
private class IntBox
{
public int IntProperty { get; set; }
}
[Test]
public void when_using_A_callTo_on_a_property_set_call()
{
var fake = A.Fake<IntBox>();
fake.IntProperty = 4;
A.CallTo(() => fake.IntProperty).WithAnyArguments().MustNotHaveHappened();
//ahhh so set calls are excluded...
// A.CallTo(() => fake.IntProperty = -1).WithAnyArguments().MustHaveHappened();
//and of course an expression cannot contain an assignment operator...
}
I would argue that the expected behavior is that
A.CallTo(() => fake.IntProperty).MustNotHaveHappened()
would check for both get
and set
calls, but this would be a breaking change in your framework and might anger a good number of people, so perhaps the simple static methods Get
and Set
on IReturnValueArgumentValidationConfiguration<T>
would suffice, so that my assertion could be:
A.CallTo.Get(() => fake.IntProperty).MustNotHaveHappened()
A.CallTo.Set(() => fake.IntProperty).MustNotHaveHappened()
Cheers!
-Geoff
In order to include patch 1.9.1 #74
If you have 2 different event handlers subscribed to the same event, when you raise the event using something like the following:
foo.SomethingHappened += Raise.With(EventArgs.Empty).Now;
.. the event handler you subscribed last will be called twice rather than each handler being called once.
When configuring fakes of delegates the Invoke-method must be explicitly specified, it should also be possible to configure the Invokes-method implicitly:
http://stackoverflow.com/questions/11518616/how-to-fake-delegates-with-fakeiteasy
These two should be equivalent:
A.CallTo(() => delegate("foo")).Returns("bar"); A.CallTo(() => delegate.Invoke("foo")).Returns("bar");
Hello,
I'm a long time fan of FakeItEasy, but one thing always bothered me about its NuGet package, and that's the fact that Castle.Core is being downloaded as a separate, external dependency.
I believe Patrick mentioned on twitter once that this is what NuGet is for - managing dependencies. And while I agree, in my opinion Castle.Core is an implementation detail of FakeItEasy. What happens if there's an older/newer version of Castle.Core already in my project? Potentially, this might break FakeItEasy.
I propose ILMerging Castle.Core directly into FakeItEasy, and simplify deployment. I believe this was done in the standalone download, before NuGet.
Thanks!
Igal.
For those of us who are using Action-based eventing, the EventArgs requirement on the Raise or Raise.With(...) stops us from using the FakeItEasy event firing. Can this be removed?
Thanks!
-Jon
Currently 10 inside VS and 6 outside.
Address warnings and then set all projects to treat all warnings as errors.
(Discussion and tracking moved from #27 raised by @skwasiborski)
The current API proposal by @patrik-hagne is
using (Fake.CreateScope())
{
A.Call.Throws(new Exception());
A.Call.WithReturnType<string>().Returns("foo");
}
I had the clever idea of updating Version.txt directly via the GitHub website in master and inadvertently added a line ending which propagated to the nuget pack command line, as can be seen from the build log http://teamcity.codebetter.com/viewLog.html?tab=buildLog&buildTypeId=bt929&buildId=67143
Just in case a line ending ever ends up in Version.txt again, we need to make rakefile.rb immune to it. It's probably a good idea to first split on line ending chars and use the first token for further operations.
FYI trying to remove the line ending via the GitHub website did not work. It resulted in a no-change commit. Note to self: never use the GitHub website to commit to master again, ever.
There was version 3.2 released. It contains some bugfixes and performance improvements.
First have a look at this minor API change, if this is relevant to you:
http://docs.castleproject.org/Windsor.Whats-New-In-Windsor-32.ashx#Revamped_property_filtering_API_breaking_change__4
could you please take a look at this so question
Thank You
All old branches which have been merged into master probably ought to be removed.
Or does anyone want to keep them for any reason?
Please see this blog post: http://www.hmemcpy.com/blog/2013/02/preventing-resharpers-implicitly-captured-closure-warning-in-fakeiteasy-unit-tests/
ReSharper will complain about the A.CallTo
methods, that they implicitly capture local variables. It's possible to prevent this, by teaching ReSharper (via external annotations, so it's completely seamless non-intrusive) that the lambdas are executed immediately by FakeItEasy.
By supplying an external annotations XML together with the DLL, this will "just work", and no code changes required anywhere.
The core FakeItEasy project has 74 warnings. Other projects may also have further warnings.
In each case we should fix the code or suppress the warning, whichever is appropriate.
We should also consider our policy for running code analysis as part of the build. The issue here is speed. Code analysis can really slow the build down. If we do choose to run code analysis as part of the build, it should fail when there are warnings, as mentioned by @philippdolder (#36 (comment)).
Summary page says 32 tests, build logs say many more.
Main points:
http://code.google.com/p/fakeiteasy/issues/detail?id=14
The folowing code generate an exception :
public interface ISetOnly
{
int MyProperty { set; }
bool MyProperty2 {set;}
}
[Test]
public void MyProperty_CanBeVerified()
{
var setOnly = A.Fake<ISetOnly>();
setOnly.MyProperty = 1;
setOnly.MyProperty2 = false;
A.CallTo(setOnly).Where(x => x.Method.Name == "set_MyProperty")
.WhenArgumentsMatch(x => x.Get<int>(0) == 1).MustHaveHappened();
}
It looks like filtering with "Where(x => x.Method.Name ...)" doesn't work.
When passing an argument to a method which uses a ByRef (VB.NET) parameter, verification of this parameter fails when the value of the argument changes.
Public Interface ISomething
Sub DoSomething(ByRef io_oSomething As Foo)
End Interface
<TestClass()>
Public Class UnitTest1
<TestMethod()>
Public Sub TestSomethingFails()
Dim oFoo As New Foo
Dim oFake As ISomething = A.Fake(Of ISomething)()
oFoo.Id = 1
oFake.DoSomething(oFoo)
oFoo.Id = 2
oFake.DoSomething(oFoo)
A.CallTo(Sub() oFake.DoSomething(A(Of Foo).That.Matches(Function(foo As Foo) foo.Id = 1))).MustHaveHappened()
End Sub
<TestMethod()>
Public Sub TestSomethingSucceeds()
Dim oFoo As New Foo(1)
Dim oFake As ISomething = A.Fake(Of ISomething)()
oFoo.Id = 1
oFake.DoSomething(oFoo)
oFoo.Id = 2
oFake.DoSomething(oFoo)
A.CallTo(Sub() oFake.DoSomething(A(Of Foo).That.Matches(Function(foo As Foo) foo.Id = 2))).MustHaveHappened(Repeated.Exactly.Twice)
End Sub
<TestMethod()>
Public Sub TestSomethingFailsToo()
Dim oFoo As New Foo(1)
Dim oFake As ISomething = A.Fake(Of ISomething)()
oFoo.Id = 1
oFake.DoSomething(oFoo)
oFoo.Id = 2
A.CallTo(Sub() oFake.DoSomething(A(Of Foo).That.Matches(Function(foo As Foo) foo.Id = 1))).MustHaveHappened()
End Sub
<TestMethod()>
Public Sub TestSomethingSucceedsToo()
Dim oFoo As New Foo(1)
Dim oFake As ISomething = A.Fake(Of ISomething)()
oFoo.Id = 1
oFake.DoSomething(oFoo)
A.CallTo(Sub() oFake.DoSomething(A(Of Foo).That.Matches(Function(foo As Foo) foo.Id = 1))).MustHaveHappened()
End Sub
End Class
There should be a way easier way to build a new version of FakeItEasy than there is today.
The output of the build should be the following:
A zip-archive containing:
There should be two NuGet packages produced, one for the signed version and one for the non signed version.
It would be great if a release build and push could be triggered by setting a tag in git on a release branch. The tag should be the version number that should be used for the build.
There is a TeamCity account set up at http://teamcity.codebetter.com/project.html?projectId=project152
UPDATE
We've decided that initially we will just produce a signed NuGet so we can get this closed out with merging/non-merging as a separate concern in #16. If we feel we need other outputs, e.g. signed/non-signed, merged/non-merged, etc. then we can raise further issue(s).
UPDATE
As part of closing this, assuming we have consolidated down to a single build config, which should switch on the 'Report change status to GitHub' build feature as detailed at http://hadihariri.com/2013/02/06/automatically-building-pull-requests-from-github-with-teamcity/
Currently we can't use this build feature because the 3 different build configs interfere with each other, i.e. the build might pass on one and fail on another and then it's a case of most recent wins with the status update on the pull request.
What steps will reproduce the problem?
var myFake = A.Fake<IMyInterface>();
What is the expected output? What do you see instead?
That the value on MyFake to be 0 not null.
What version of the product are you using? On what operating system?
Tried on version 1.7.4257.42 (other: c# / windows 7 / visual studio 2010 ultimate)
Please provide any additional information below.
Code:
public interface IMyInterface : IYourInterface
{
decimal? Number { get; set; }
}
public interface IYourInterface
{
decimal? Number { get; set; }
}
public class SomeClass
{
public void SetValues(IYourInterface yi)
{
yi.Number = 0;
}
}
The below example fails with the following error message:
Attempt by method 'Castle.Proxies.PersonProxy.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)' to access method 'Castle.DynamicProxy.Generators.Emitters.TypeUtil.Sort(System.Reflection.MemberInfo[])' failed.
class Program
{
static void Main(string[] args)
{
var person = A.Fake<Person>();
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
formatter.Serialize(stream, person);
}
}
}
[Serializable]
public class Person
{}
There are tests that asserts that fakes can be serialized so this must have to do with the IL-merge.
Will be released as patch 1.9.1.
If we move the website from the gh-pages branch of this repo to its own fakeiteasy.github.com repo then it will be addressable by http://fakeiteasy.github.io/
The Raise<>.With
syntax is typed to events with the EventArgs
convention.
There are many cases where I use events with custom delegate types, like such
public delegate void MyDelegate(MyCargo c);
// ...
public event MyDelegate Foo;
Unfortunately I cannot raise such an event on a Fake with the given Raise<>
API.
A way to raise any type of delegate/cargo would be very useful; meaning if Raise<>
took a custom/generic delegate signature and didn't force the (sender,EventArgs)
pattern.
The use case I imagine (or, really, expected at first) was this (continuing the above delegate example):
var f = A.Fake<IFireEvents>();
f.Foo += Raise.With<MyDelegate>(new Cargo()).Now;
or maybe
f.Foo += Raise.With<MyCargo>(new Cargo()).Now;
where, in the latter case, Raise would expose an Action<TCargo>
or similar.
Not sure how much of this is really possible, but depending on how desperate I get I might try to hack it up myself in a fork.
However, I'm sure you guys know what to do in this case much better than I do :-)
Thanks for the great tool!
I noticed this first on the build server in this build http://teamcity.codebetter.com/viewLog.html?buildId=64653&tab=buildResultsDiv&buildTypeId=bt483
The next build succeeded (with no changes) so I suspected an intermittent bug. I then tried running the test repeatedly in Visual Studio using TestDriven.NET and sure enough, after about 10 executions I got
Test 'FakeItEasy.Tests.Core.FakeManagerTests.Rules_should_only_be_valid_within_the_current_scope' failed: System.NullReferenceException : Object reference not set to an instance of an object.
Core\FakeManagerTests.cs(281,0): at FakeItEasy.Tests.Core.FakeManagerTests.Rules_should_only_be_valid_within_the_current_scope()
The interface IExceptionThrowerConfiguration has a method "Throws", this should instead be "ThrowsLazily" that takes a Func. The current Throws method should be an extension method for this interface instead.
Ready when all other issues forming part of the release are Done.
https://nuget.org/packages/StyleCop.MSBuild/
We can also add
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
in the csproj files if we want to enforce compliance.
See https://help.github.com/articles/dealing-with-line-endings
I've still seen a few files cropping up with mixed line endings even when using autocrlf=true, which is strange.
I think if we do a one-off normalisation and enforcement using gitattributes then we should kill this problem once and for all.
Note that we need to run a one-off build on the build server with 'clean all files in checkout directory before build' switched on after making this change.
Attempting to resolve dependency 'Castle.Core (โฅ 3.1.0.0)'.
Successfully installed 'Castle.Core 3.1.0'.
Successfully installed 'FakeItEasy 1.7.4626.65'.
The process cannot access the file 'C:\Project\Castle.Core.3.1.0\lib\net40-client\Castle.Core.dll' because it is being used by another process.
The directory is not empty.
Successfully uninstalled 'Castle.Core 3.1.0'.
Install failed. Rolling back...
Failed to add reference to 'Castle.Core'.
Keep getting this message when installing FakeItEasy, also same error when installing Castle.Core, maybe this should be sent to castle.Core but thought you might want to know...
Ready when all other issues forming part of the release are Done.
I have a method with the following signature.
Foo GetFooById( int id, params string[] children )
This method is defined on an interface named IDal.
In my unit test I write the following:
IDal dal = A.Fake<IDal>();
Foo fooToReturn = new Foo();
fooToReturn.Id = 7;
A.CallTo(()=>dal.GetFooById(7, "SomeChild")).Returns(fooToReturn);
When the test runs, the signature isn't being matched on the second argument. I tried changing it to:
A.CallTo(()=>dal.GetFooById(7, new string[]{"SomeChild"})).Returns(fooToReturn);
But that was also unsuccessful. The only way I can get this to work is to either ignore the second argument:
A.CallTo(()=>dal.GetFooById(7, A<string[]>.Ignored )).Returns(fooToReturn);
Or to use IsSameSequenceAs:
A.CallTo(()=>dal.GetFooById(7, A<string[]>.That.IsSameSequenceAs(new string[] { "SomeChild" }) )).Returns(fooToReturn);
The latter works, but is a little long and unwieldly.
If FakeItEasy could handle params arguments more naturally (like the framework does) it would make this type of situation more intuitive.
I want so see how much (if any) objection there is to this.
Silverlight 3 is not supported in Visual Studio 2012. This means continued support for SL3 puts an extra burden on developers to maintain an installation of an obsolete IDE (VS2010).
Ready when all other issues forming part of the release are Done.
Do we really need a separate nuget package for the .NET 3.5 build?
Parallel test execution is not currently supported.
If it turned on, ExpectationException (Call to non configured method) errors appear :(
I'm still experiencing problems when updating Castle.Windsor. Are there considerations about il-merging the specific Castle version into FakeItEasy? Or what would be the best solution if i want to keep my Castle version up to date?
when I debug MSUnit test code I get
System.MissingMethodException occurred
Message=Cannot create an instance of an interface.
Source=mscorlib
StackTrace:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
InnerException:
on the first A.Fake<ITest>()
call
ITest tt = A.Fake<ITest>();
It runs normally when I just run tests. so it happens only under visual studio debugger. what is is missing?
windows7/VS2010 ultimate/FakeItEasy 1.7.4626.65
Started recently... not sure what changed.
with guideline for contributors, e.g line endings, branching, etc.
This is a special file that GitHub knows about and the UI tells fork creators about it.
Either by fixing issues, or changing the analysis settings in each case. Preferably the former, we should try and stick to the default StyleCop settings as much as possible so that we adopt a coding style which is more compatible with other projects.
Once this is done, we should add <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
to the properties in the csproj files so that the build fails on violations.
Maybe this is too much overhead for a simple benefit but I'm dumping it here anyway as an idea. Just thought it might be cool to support fluent assertions which would allow to use their exception and assertion model. The one could use:
A.CallTo().Should().HaveHappened().And.ExactlyOnce()
This would also help to spread the usage of FluentAssertions
Please signal somewhere in http://code.google.com/p/fakeiteasy/ that the current repository is this one and not google code's hg repository. A usual practice is also to create a new commit removing all files from the old repository except for a README that points to the new repository.
The easiest way to do this is by using https://travis-ci.org which has this built in. We'd need to be building for Mono since their build environment currently only consists of Linux machines. I think it would also be easiest if we used Rake.
This can also be done on TeamCity, see http://hadihariri.com/2013/02/06/automatically-building-pull-requests-from-github-with-teamcity/
For now, let's go with TeamCity. We can think about using Travis CI later if we either start to build for Mono or if Travis CI starts to provide Windows builds.
var foo = A.Fake<IFoo>();
A.CallTo(() => foo.Baz()).ReturnsLazily((object fake, object arg) => 0);
foo.Baz();
throws an exception with the message
The faked method has the signature (), but invokes was used with (System.Object, System.Object).
The exception message should be
The faked method has the signature (), but returns lazily was used with (System.Object, System.Object).
In Rhino Mocks, there is a way to specify a dummy ref parameter like so:
ref Arg<string>.Ref(Rhino.Mocks.Constraints.Is.Equal(string.Empty), "New value").Dummy
While I generally dislike ref parameters (especially for unit test purposes), there is no way around legacy APIs or third-party code.
What do you think about this syntax:
ref A<string>.That.IsEqualTo(string.Empty).ChangesTo("New value").Dummy
// extension method signature
public static Dummy<T> ChangesTo<T>(this T value, T newValue);
// class
public class Dummy<T>
{
public T Dummy;
}
This obviously is a draft and will require a bit of polishing...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.