rowanmiller / entityframework.testing Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Add(AddAccountViewModel model)
{
var user = new User(model.Username, model.Email, model.FirstName, model.LastName);
_dataContext.Users.Add(user);
_dataContext.SaveChanges();
return RedirectToAction("Login", new { Created = true });
}
[Fact]
public void AccountController_Add_SuccessfullyAddsUser()
{
//Arrange
var set = new MockDbSet<User>()
.SetupLinq();
var context = new Mock<DataContext>();
context.Setup(c => c.Users).Returns(set.Object);
var authenticator = new Mock<IAuthenticator>();
var controller = new AccountController(authenticator.Object, context.Object);
//Act
var result = (RedirectToRouteResult)controller.Add(new AddAccountViewModel { Username = "foo", Email = "bar", FirstName = "baz", LastName = "blah", Password = "what"});
//Assert
Assert.Equal(1, context.Object.Users.Count());
}
Xunit.Sdk.EqualExceptionAssert.Equal() Failure
Expected: 1
Actual: 0
The following code runs in production but throws an InvalidOperationException in the unit test when DBSet (Materials) is a MockDBSet. Here is the setup for the mocks:
var items = new List<Material> {
new Material { Id = 1, Pieces = 10 },
new Material {Id = 2, Pieces = 10 }
};
var mockItems = new MockDBSet<Material>()
.SetupSeedData(items)
.SetupAddAndRemove()
.SetupLinq();
var mockContext = new Mock<MyContext>();
mockContext.Setup(o=>o.Materials).Returns(mockItems.Object);
The mockContext is then passed to a service. Here is the code within the service that throws the exception:
var items = _context.Materials.ToList();
var newItem = new Material();
var oldItem = items.First();
_context.Materials.Add(newItem);
_context.Entry(newItem).CurrentValues.SetValues(oldItem);
Here is the exception:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Member 'CurrentValues' cannot be called for the entity of type 'Material' because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet.
I'm curious as to why I have to use a specific version of Moq when using these (EntityFramework.Testing and EntityFramework.Testing.Moq) libraries? I seem to have to install Moq version 4.2.1402.2112 in order to get the tests to run successfully. Is this intended?
Hey Rowan.
Is there any documentation or examples of usage for this project? An introductory blog post perhaps?
p.s. I got damn fucking love this little package!
Thanks.
At the moment I have to use Enumerable.SingleOrDefault
instead of DbSet.Find
because otherwise my tests fail.
It would be nice to have support for DbSet.Find
.
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.