GithubHelp home page GithubHelp logo

entityframework.testing's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

entityframework.testing's Issues

Support DbSet.Add/Remove

[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

DBContext.Entry(newEntity).CurrentValues.SetValues(oldEntity) throws InvalidOperationException

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.

Why specific version of Moq?

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?

[email protected]
@JohnMarkHowell

Documentation

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.

Support for DbSet.Find

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.