Skip to content

Instantly share code, notes, and snippets.

@AlexZeitler
Created April 13, 2011 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexZeitler/917497 to your computer and use it in GitHub Desktop.
Save AlexZeitler/917497 to your computer and use it in GitHub Desktop.
Configuring the The<>() directly
Running the tests results in "System.InvalidOperationException:
The result for IContactRepository.Get(1); has already been setup."
When creating An<IContactRepository> and assigning it by fakeAccessor.Use(), it does work.
public class When_requesting_alex_from_contact_service : WithSubject<ContactService> {
static Contact _contact;
static IContact _actualContact;
Establish context = () => {
With<Alex>();
With<AlexRepository>();
};
Because of = () => { _actualContact = Subject.GetContact(1); };
It should_return_the_contact = () => { _actualContact.ShouldEqual(The<IContact>()); };
}
public class AlexRepository {
OnEstablish context =
fakeAccessor
=> {
fakeAccessor.The<IContactRepository>()
.WhenToldTo(r => r.Get(1))
.Return(fakeAccessor.The<IContact>);
};
}
public class Alex {
OnEstablish context = fakeAccessor => {
fakeAccessor.The<IContact>().Id = 1;
fakeAccessor.The<IContact>().Name = "Alex";
};
}
public interface IContact {
string Name { get; set; }
int Id { get; set; }
}
public class Contact : IContact {
public string Name { get; set; }
public int Id { get; set; }
}
public class ContactService {
readonly IContactRepository _contactRepository;
public ContactService(IContactRepository contactRepository) {
_contactRepository = contactRepository;
}
public IContact GetContact(int id) {
return _contactRepository.Get(id);
}
}
public interface IContactRepository {
IContact Get(int id);
}
@BjRo
Copy link

BjRo commented Apr 13, 2011

Yap, that's definately a bug. Has already been fixed on the dev branch and will be part of the next nuget release: https://github.com/BjRo/Machine.Fakes/commit/8fd49b78f9e5073bc43488e5ee561e9fd2513c1e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment