ASP.NET Web Api CRUD operation in VS 2010 web application - entity-framework

I tried to make ASP.NET Web Api CRUD operation in VS 2010 web application, but why the result is not returning all entire row from source table.
This is my code :
Route/Globax.asax
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}", // browse with localhost:7031/api/product
//routeTemplate: "{controller}/{id}", // browse with localhost:7031/product
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
Controller/ProductController.cs :
public class ProductController : ApiController
{
NorthwindEntities db = new NorthwindEntities();
public List<Product> GetAll()
{
return db.Products.ToList<Product>();// ;
}
View/ViewProduct.aspx :
<script src="Script/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#<%= cviewproduct.ClientID %>').click(function (e) {
getProducts();
e.preventDefault();
});
});
function getProducts() {
$.getJSON("/api/product",
function (data) {
$.each(data, function (key, val) {
//var str = val.ProductName;
// alert(str);
var row = '<tr> <td>' + val.ProductName + '</td><td>' + val.ProductID + '</td><tr/>';
$(row).appendTo($('#tblproduct'));
});
});
}
</script>
Bellow Is The Result of product Controller via 'http://localhost:7031/api/product' :
Bellow Is The Result of getProducts() function :
Please help me.
Any idea or suggestion ?

When you execute $.getJSON("/api/product", ..., you are not getting back XML as you posted. You are getting back JSON.
As a first step, I suggest you download and install Fiddler2. Open it, and use the Composer tab to execute a GET for http://localhost:7031/api/product. That should show you the JSON returned, which will be different from the XML. Post that JSON to your original question and we should be able to help further.
My guess is that the JSON is not properly formatted. What does your WebApiConfig.cs class look like?
Update
Yes, there is a better way. First, create an ApiModel (ApiModel is to WebAPI what ViewModel is to MVC):
public class ProductApiModel
{
public int ProductId { get; set; }
public string ProductName { get; set; }
}
Now, return this instead of an entity from your controller:
public class ProductController : ApiController
{
public IEnumerable<ProductApiModel> GetAll()
{
var products = db.Products
.OrderBy(x => x.ProductID)
.Select(x => new ProductApiModel
{
ProductId = x.ProductID,
ProductName = x.ProductName
});
return products;
}
}
If you used AutoMapper, you could make your controller code a bit shorter:
public class ProductController : ApiController
{
public IEnumerable<ProductApiModel> GetAll()
{
var entities = db.Products.OrderBy(x => x.ProductID);
var models = Mapper.Map<ProductApiModel[]>(entities);
return models;
}
}

After do some research, i finaly found (Please Correct Me, if i'm wrong) :
JavaScriptSerializer failed to serialize the entire objects tree from relations
between the entity and other entities (Product and Catagory).
So .. I made ​​some changes to my code, and the results are as expected
Controller/ProductController.cs :
From :
public class ProductController : ApiController
{
NorthwindEntities db = new NorthwindEntities();
public List<Product> GetAll()
{
return db.Products.ToList<Product>();// ;
}
To :
public class ProductController : ApiController
{
public string GetAll()
{
var product = db.Products.OrderBy(x => x.ProductID).Select(x => new
{
ProductId = x.ProductID,
ProductName = x.ProductName
});
return JsonConvert.SerializeObject(product);
}
View/ViewProduct.aspx :
From :
function getProducts() {
$.getJSON("/api/product",
function (data) {
$.each(data, function (key, val) {
var row = '<tr> <td>' + val.ProductName
+ '</td><td>' + val.ProductID + '</td><tr/>';
$(row).appendTo($('#tblproduct'));
});
});
To :
$.getJSON("/api/product",
function (data) {
var obj = $.parseJSON(data);
$.each(obj, function (key, val) {
var row = '<tr> <td>' + val.ProductId
+ '</td><td>' + val.ProductName + '</td><tr/>';
$(row).appendTo($('#tblproduct'));
});
});
Or... is there a better way than this,
if I still want to pass an entity object instead of a string object
(List GetAll ()... instead GetAll string ()....)
Regards,
Andrian

So.. This My Final Working Code
Models :
namespace MyLabs1.Models
{
public class ProductApi
{
[Key]
public Int32 ProductID { get; set; }
public string ProductName { get; set; }
}
}
Controller/ProductController.cs :
public IEnumerable<ProductApi> getall()
{
Mapper.CreateMap<Product, ProductApi>();
var entities = db.Products.OrderBy(x => x.ProductID).ToList();
var models = Mapper.Map<ProductApi[]>(entities);
return models;
}
OR
public List<ProductApi> GetAll()
{
var products = db.Products
.OrderBy(x => x.ProductID)
.Select(x => new ProductApi
{
ProductID = x.ProductID,
ProductName = x.ProductName
}).ToList();
return products;
}
View/ViewProduct.aspx :
function getProducts() {
$.getJSON("/api/product",
function (data) {
$.each(data, function (key, val) {
var row = '<tr> <td>' + val.ProductName + '</td><td>' + val.ProductID + '</td><tr/>';
$(row).appendTo($('#tblproduct'));
});
});
}
Hope This Will Be Helpful to Others
Regards,
Andrian

Related

Subscribe to Close, but close only if item was saved

My scenario:
In an MVVM pattern, the view should be closed when a command is executed on ViewModel, but only if the item was successfully saved.
The View looks like:
public class CentreUpdateWindow : ReactiveWindow<CentreUpdateViewModel>
{
public CentreUpdateWindow()
{
this.InitializeComponent();
this.WhenActivated(d =>
d(ViewModel!.SubmitCommand.Subscribe(CloseIfSuccessfullySaved))
);
}
private void CloseIfSaved(Centre? obj)
{
if (ViewModel!.SuccessfullySaved)
Close(obj);
}
// ...
And the ViewModel:
public class CentreUpdateViewModel : ViewModelBase, IId
{
// ...
public ReactiveCommand<Unit, dtoo.Centre?> SubmitCommand { get; }
private bool _SuccessfullySaved;
public bool SuccessfullySaved
{
get { return _SuccessfullySaved; }
protected set { this.RaiseAndSetIfChanged(ref _SuccessfullySaved, value); }
}
The question:
The code works fine, but I'm not comfortable with the if (ViewModel!.SuccessfullySaved). I guess should be a way to write subscribe expression more accurate.
Is there a more elegant way to Subscribe on WhenActivated more "reactiveuistic" ?
public CentreUpdateWindow()
{
this.InitializeComponent();
this.WhenActivated(d =>
d(
ViewModel
.WhenAnyValue(x => x.SuccessfullySaved)
.CombineLatest(ViewModel!.SubmitCommand,
(saved, obj) => (saved, obj))
.Where(s => s.saved)
.Select(s => s.obj)
.Subscribe(Close)
));
}

Rx.Net: Chaining subscribers - alternative approach?

How can I re-write this code so that I don't have to chain Subscribers like below? Reason for asking is, this style will limit in an observable depending on another observable due to the style of the code, it can get confusing.
var results = myService
.GetData(accountId) // returns IObservable
.Subscribe(data =>
{
new MyWork().Execute(data) // returns IObservable
.Subscribe(result =>
{
myResults.Add(result);
WriteLine($"Result Id: {result.Id}");
WriteLine($"Result Status: {result.Pass}");
});
});
Added after 1st reply from Peter Bons
Below is the code for MyWork class that has the Execute Method
public class MyWork
{
public virtual IObservable<MyResult> Execute(MyData data)
{
MyResult result = null;
return IsMatch(data)
.Do(isMatch =>
{
if (isMatch)
{
result = new MyResult(1, true);
}
})
.Select(_ => result);
}
public IObservable<bool> IsMatch(MyData data)
{
return true;
}
}
It's really quite simple.
var results =
myService
.GetData(accountId)
.SelectMany(data => new MyWork().Execute(data))
.Subscribe(result =>
{
myResults.Add(result);
Console.WriteLine($"Result Id: {result.Id}");
Console.WriteLine($"Result Status: {result.Pass}");
});
If ever you are subscribing within a subscription then you are doing something wrong. Keep that in mind. There is almost always a way to make it a pure query with a single subscription.
Just to help out with testing, here's the code required to make this a Minimal, Complete, and Verifiable example.
public static class myService
{
public static IObservable<MyData> GetData(int x)
=> Observable.Return(new MyData());
}
public class MyWork
{
public virtual IObservable<MyResult> Execute(MyData data)
{
MyResult result = null;
return IsMatch(data)
.Do(isMatch =>
{
if (isMatch)
{
result = new MyResult() { Id = 1, Pass = true};
}
})
.Select(_ => result);
}
public IObservable<bool> IsMatch(MyData data)
{
return Observable.Return(true);
}
}
public class MyResult
{
public int Id;
public bool Pass;
}
public class MyData { }

Create unit test for View() with a List in ASP.net core MVC application

I am new in unit testing. I have a controller - StudentsController with dependency injection and there my Index() method:
public class StudentsController : Controller
{
public readonly UniversityContext _context;//Database
public StudentsController(UniversityContext context)
{
_context = context;
}//Constructor with database
// GET: Students
public async Task<IActionResult> Index()
{
return View(await _context.Students.ToListAsync());
}
}
Next i need to write a correct unit test code, that check, if:
1) a View() have a list with my students
2) The query with students is not null.
I read about Mock objects, but I don't know how to write the correct code. My code that I wrote so far:
public class StudentsControllerTests
{
[Fact]
public async Task Index_ReturnsAViewResult_WithAListOfStudents()
{
var mockRepo = new Mock<UniversityContext>();
mockRepo.Setup(repo => repo.Students.ToList()).Returns(GetTestStudents());//There i get following error:Expression references a method that does not belong to the mocked object
var controller = new StudentsController(mockRepo.Object);
// Act
var result = controller.Index();
//// Assert
var viewResult = Assert.IsType<ViewResult>(result);
var model = Assert.IsAssignableFrom<IEnumerable<Student>>(
viewResult.ViewData.Model);
Assert.NotNull(model);//Second Condition
}
public List<Student> GetTestStudents()
{
var sessions = new List<Student>();
sessions.Add(new Student()
{
bDate = new DateTime(1994, 7, 2),
Name = "Test One"
});
sessions.Add(new Student()
{
bDate = new DateTime(1995, 7, 1),
Name = "Test Two"
});
return sessions;
}
}
Can someone explain me, how to correct my code?
You only need to mock the members of the context, which in this case is the .Students property. ToList is an extension method being call on the property and cannot be mocked by moq.
Also .Students is a DbSet and would need to be mocked as well.
Using the test classes from this answer :
How to mock an async repository with Entity Framework Core
The following generic extension methods were derived
public static class MockDbSetExtensions {
public static Mock<DbSet<T>> AsDbSetMock<T>(this IEnumerable<T> list) where T : class {
IQueryable<T> queryableList = list.AsQueryable();
Mock<DbSet<T>> dbSetMock = new Mock<DbSet<T>>();
dbSetMock.As<IQueryable<T>>().Setup(x => x.Provider).Returns(queryableList.Provider);
dbSetMock.As<IQueryable<T>>().Setup(x => x.Expression).Returns(queryableList.Expression);
dbSetMock.As<IQueryable<T>>().Setup(x => x.ElementType).Returns(queryableList.ElementType);
dbSetMock.As<IQueryable<T>>().Setup(x => x.GetEnumerator()).Returns(() => queryableList.GetEnumerator());
return dbSetMock;
}
public static Mock<DbSet<T>> ToAsyncDbSetMock<T>(this IEnumerable<T> source)
where T : class {
var data = source.AsQueryable();
var mockSet = new Mock<DbSet<T>>();
mockSet.As<IAsyncEnumerable<T>>()
.Setup(m => m.GetEnumerator())
.Returns(new TestAsyncEnumerator<T>(data.GetEnumerator()));
mockSet.As<IQueryable<T>>()
.Setup(m => m.Provider)
.Returns(new TestAsyncQueryProvider<T>(data.Provider));
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
return mockSet;
}
}
With the above utilities, update
mockRepo.Setup(repo => repo.Students.ToList()).Returns(GetTestStudents());
To
var studentsMockedDbSet = GetTestStudents().ToAsyncDbSetMock();
mockRepo.Setup(repo => repo.Students).Returns(studentsMockedDbSet.Object);

Image Carousel using Twitter Bootstrap and Orchard CMS

I am using Orchard CMS and the Bootstrap theme. I have been trying to use the carousel built in bootstrap to work as shown in the following blog post: http://www.stevetaylor.me.uk/image-carousel-using-twitter-bootstrap-and-orchard-cms-projections. I have followed the tutorial but i cannot get my new layout file to appear in the query under grid and html list. I believe i have copied the code word for word but still can get it to work. Anybody please help with this as think it would be a great feature to add. if i get it to work i will request it gets added to the bootstrap theme here: http://orchardbootstrap.codeplex.com/
See code below:
CarouselLayoutForm.cs
using System;
using Orchard.DisplayManagement;
using Orchard.Forms.Services;
using Orchard.Localization;
namespace Orchard.Projections.Providers.Layouts {
public class CarouselLayoutForms : IFormProvider {
protected dynamic Shape { get; set; }
public Localizer T { get; set; }
public CarouselLayoutForms(
IShapeFactory shapeFactory) {
Shape = shapeFactory;
T = NullLocalizer.Instance;
}
public void Describe(DescribeContext context) {
Func<IShapeFactory, object> form =
shape => {
var f = Shape.Form(
Id: "CarouselLayout",
_HtmlProperties: Shape.Fieldset(
Title: T("Html properties"),
_ListId: Shape.TextBox(
Id: "outer-grid-id", Name: "OuterDivId",
Title: T("Outer div id"),
Description: T("The id to provide on the div element."),
Classes: new[] { "textMedium", "tokenized" }
),
_ListClass: Shape.TextBox(
Id: "outer-div-class", Name: "OuterDivClass",
Title: T("Outer div class"),
Description: T("The class to provide on the div element."),
Classes: new[] { "textMedium", "tokenized" }
),
_InnerClass: Shape.TextBox(
Id: "inner-div-class", Name: "InnerDivClass",
Title: T("Inner div class"),
Description: T("The class to provide on the inner div element."),
Classes: new[] { "textMedium", "tokenized" }
),
_FirstItemClass: Shape.TextBox(
Id: "first-item-class", Name: "FirstItemClass",
Title: T("First item class"),
Description: T("The class to provide on the first item element."),
Classes: new[] { "textMedium", "tokenized" }
),
_ItemClass: Shape.TextBox(
Id: "item-class", Name: "ItemClass",
Title: T("Item class"),
Description: T("The class to provide on the item element."),
Classes: new[] { "textMedium", "tokenized" }
)
)
);
return f;
};
context.Form("CarouselLayout", form);
}
}
/*
public class CarouselLayoutFormsValitator : FormHandler {
public Localizer T { get; set; }
public override void Validating(ValidatingContext context) {
if (context.FormName == "CarouselLayout") {
if (context.ValueProvider.GetValue("Alignment") == null) {
context.ModelState.AddModelError("Alignment", T("The field Alignment is required.").Text);
}
if (context.ValueProvider.GetValue("Columns") == null) {
context.ModelState.AddModelError("Columns", T("The field Columns/Lines is required.").Text);
}
else {
int value;
if (!Int32.TryParse(context.ValueProvider.GetValue("Columns").AttemptedValue, out value)) {
context.ModelState.AddModelError("Columns", T("The field Columns/Lines must be a valid number.").Text);
}
}
}
}
}
*/
}
CarouselLayout.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Projections.Descriptors.Layout;
using Orchard.Projections.Models;
using Orchard.Projections.Services;
namespace Orchard.Projections.Providers.Layouts {
public class CarouselLayout : ILayoutProvider {
private readonly IContentManager _contentManager;
protected dynamic Shape { get; set; }
public CarouselLayout(IShapeFactory shapeFactory, IContentManager contentManager) {
_contentManager = contentManager;
Shape = shapeFactory;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(DescribeLayoutContext describe) {
describe.For("Html", T("Html"),T("Html Layouts"))
.Element("Carousel", T("Carousel"), T("Organizes content items in a carousel."),
DisplayLayout,
RenderLayout,
"CarouselLayout"
);
}
public dynamic RenderLayout(LayoutContext context, IEnumerable<LayoutComponentResult> layoutComponentResults) {
string outerDivClass = context.state.outerDivClass;
string OuterDivId = context.state.OuterDivID;
string innerDivClass = context.state.InnerDicClass;
string firstItemClass = context.state.FirstItemClass;
string itemClass = context.state.ItemClass;
IEnumerable<dynamic> shapes =
context.LayoutRecord.Display == (int)LayoutRecord.Displays.Content
? layoutComponentResults.Select(x => _contentManager.BuildDisplay(x.ContentItem, context.LayoutRecord.DisplayType))
: layoutComponentResults.Select(x => x.Properties);
return Shape.Carousel(Id: outerDivId, Items: shapes, OuterClasses: new[] { outerDivClass },
InnerClasses: new[] {innerDivClass}, FirstItemClasses: new[] {firstItemClass}, ItemClasses: new[] {itemClass});
}
}
}
LayoutShapes.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc.Html;
using Orchard.Utility.Extensions;
namespace Orchard.Projections.Providers.Layouts {
public class LayoutShapes : IDependency {
public LayoutShapes() {
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
[Shape]
public void Carousel(dynamic Display, TextWriter Output, HtmlHelper Html, string Id, IEnumerable<dynamic> Items,
IEnumerable<string> OuterClasses, IDictionary<string, string> OuterAttributes,
IEnumerable<string> InnerClasses, IDictionary<string, string> InnerAttributes,
IEnumerable<string> FirstItemClasses, IDictionary<string, string> FirstItemAttributes, IEnumerable<string> ItemClasses, IDictionary<string, string> ItemAttributes )
{
if (Items == null) return;
var items = Items.ToList();
var itemsCount = items.Count;
if (itemsCount < 1) return;
var outerDivTag = GetTagBuilder("div", Id, OuterClasses, OuterAttributes);
var innerDivTag = GetTagBuilder("div", string.Empty, InnerClasses, InnerAttributes);
var firstItemTag = GetTagBuilder("div", string.Empty, FirstItemClasses, FirstItemAttributes);
var itemTag = GetTagBuilder("div", string.Empty, ItemClasses, ItemAttributes);
Output.Write(outerDivTag.ToString(TagRenderMode.StartTag));
Output.Write(innerDivTag.ToString(TagRenderMode.StartTag));
int i = 0;
foreach (var item in items)
{
if (i== 0)
Output.Write(firstItemTag.ToString(TagRenderMode.StartTag));
else
Output.Write(itemTag.ToString(TagRenderMode.StartTag));
Output.Write(Display(item));
Output.Write(itemTag.ToString(TagRenderMode.EndTag));
i++;
}
Output.Write(innerDivTag.ToString(TagRenderMode.EndTag));
Output.Write("‹",id);
Output.Write("‹",id);
Output.Write(outerDivTag.ToString(TagRenderMode.EndTag));
Output.Write("<script>$(function () {$('"+ Id +"').carousel();}); </script>");
}
static TagBuilder GetTagBuilder(string tagName, string id, IEnumerable<string> classes, IDictionary<string, string> attributes) {
var tagBuilder = new TagBuilder(tagName);
tagBuilder.MergeAttributes(attributes, false);
foreach (var cssClass in classes ?? Enumerable.Empty<string>())
tagBuilder.AddCssClass(cssClass);
if (!string.IsNullOrWhiteSpace(id))
tagBuilder.GenerateId(id);
return tagBuilder;
}
}
}
Did you add your code to the module Orchard.Projections as explained?
You need to add the files in Visual Studio and to save the project for them to be included in the .csproj (unless, the dynamic compilation won't take the files in account), or you can build the module.
You are missing the definition for DisplayLayout() in your CarouselLayou.cs file:
public LocalizedString DisplayLayout(LayoutContext context)
{
string columns = context.State.Columns;
bool horizontal = Convert.ToString(context.State.Alignment) != "vertical";
return horizontal
? T("{0} columns grid", columns)
: T("{0} lines grid", columns);
}
Also, variable 'id' in LayourShapes.cs should be 'Id', and there are other incorrect casings in the declarations:
string outerDivClass = context.state.outerDivClass;
string OuterDivId = context.state.OuterDivID;
string innerDivClass = context.state.InnerDicClass;
string firstItemClass = context.state.FirstItemClass;
string itemClass = context.state.ItemClass;
should be:
string outerDivClass = context.State.OuterDivClass;
string outerDivId = context.State.OuterDivID;
string innerDivClass = context.State.InnerDicClass;
string firstItemClass = context.State.FirstItemClass;
string itemClass = context.State.ItemClass;

How do you mock adding items to a repository or DbContext using moq?

The examples I've seen for using moq for a repository only show how to mock things being returned. I have a somewhat strange requirement: when a query is executed, if a condition exists, a certain item should be added to the repository. I am wondering how to test this without querying the database. I know how to mock the condition existing, but then how do you setup the mock so that you can test that the certain item is added?
Try to use fake in memory repository instead of moq, for example universal generic repository for all entities:
public interface IInMemoryRepository<T> where T : class
{
IQueryable<T> GetAll();
void Create(T item);
void Update(T item);
T GetItem(Expression<Func<T, bool>> expression);
void Delete(T item);
}
public class InMemoryRepository<T> : IInMemoryRepository<T> where T : class
{
private int _incrementer = 0;
public Dictionary<int, T> List = new Dictionary<int, T>();
public IQueryable<T> GetAll()
{
return List.Select(x => x.Value).AsQueryable();
}
public void Create(T item)
{
_incrementer++;
item.GetType().GetProperties().First(p => p.Name == "Id").SetValue(item, _incrementer, null);
List.Add(_incrementer, item);
}
public void Update(T item)
{
var key = (int)item.GetType().GetProperties().First(p => p.Name == "Id").GetValue(item, null);
List[key] = item;
}
public T GetItem(Expression<Func<T, bool>> expression)
{
return List.Select(x => x.Value).SingleOrDefault(expression.Compile());
}
public void Delete(T item)
{
var key = (int)item.GetType().GetProperties().First(p => p.Name == "Id").GetValue(item, null);
List.Remove(key);
}
}
You would not mock the repository; you would have an alternate repository that would use an in-memory store instead of the database, then use IoC to select the correct repository implementation for tests/code.
This blog article might be of use, although my design has changed somewhat since I wrote the post and I really need to update it. I used teh generic repository pattern in a way that enables the DbContext to be mocked. This allows the data access layer to be tested 'right up to the edges'.
Times have changed -- since the release of Entity Framework 6 it has become much easier to mock database context and datasets. This article outlines the particulars.
Testing non-query scenarios
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Data.Entity;
namespace TestingDemo
{
[TestClass]
public class NonQueryTests
{
[TestMethod]
public void CreateBlog_saves_a_blog_via_context()
{
var mockSet = new Mock<DbSet<Blog>>();
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(m => m.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
service.AddBlog("ADO.NET Blog", "http://blogs.msdn.com/adonet");
mockSet.Verify(m => m.Add(It.IsAny<Blog>()), Times.Once());
mockContext.Verify(m => m.SaveChanges(), Times.Once());
}
}
}
Testing query scenarios
Query testing is pretty sweet now, because you can build up test data sets in code and then execute your tests against them:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
namespace TestingDemo
{
[TestClass]
public class QueryTests
{
[TestMethod]
public void GetAllBlogs_orders_by_name()
{
var data = new List<Blog>
{
new Blog { Name = "BBB" },
new Blog { Name = "ZZZ" },
new Blog { Name = "AAA" },
}.AsQueryable();
var mockSet = new Mock<DbSet<Blog>>();
mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(0 => data.GetEnumerator());
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(c => c.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
var blogs = service.GetAllBlogs();
Assert.AreEqual(3, blogs.Count);
Assert.AreEqual("AAA", blogs[0].Name);
Assert.AreEqual("BBB", blogs[1].Name);
Assert.AreEqual("ZZZ", blogs[2].Name);
}
}
}
You can do this by mocking the DbSet.Add() method, like so:
[Fact]
public void CreateBlog_saves_a_blog_via_context()
{
var data = new List<Blog>
{
new Blog { Name = "BBB" },
new Blog { Name = "ZZZ" },
new Blog { Name = "AAA" },
};
var mockSet = new Mock<DbSet<Blog>>();
mockSet.Setup(blogs => blogs.Add(It.IsAny<Blog>)).Returns<Blog>(blog =>
{
data.Add(blog);
return blog;
});
var mockContext = new Mock<BloggingContext>();
mockContext.Setup(m => m.Blogs).Returns(mockSet.Object);
var service = new BlogService(mockContext.Object);
var blog = service.AddBlog("_ADO.NET Blog", "http://blogs.msdn.com/adonet");
var blogs = service.GetAllBlogs();
mockSet.Verify(m => m.Add(It.IsAny<Blog>()), Times.Once());
mockContext.Verify(m => m.SaveChanges(), Times.Once());
Assert.NotNull(blog)
Assert.Equal(4, blogs.Count);
Assert.Equal("AAA", blogs(1).Name);
Assert.Equal("BBB", blogs(2).Name);
Assert.Equal("ZZZ", blogs(3).Name);
}
This is adapted from the documentation found here.