I solved my problem, but am not sure why things are working and am asking because I always want to know why things work the way they do.
Ok, I am developing an MVC 3 application am utilizing a generic repository, unity for DI, and moq for unit testing. In my unit test for editing an item, my controller initially had this as follows:
AnEntity obj = _anEntityService.GetById(id);
however, when my unit test was calling the Edit(1) in the controller, null was being returned. When I changed the read to be
AnEntity obj = _anEntityService.GetAll().FirstOrDefault(p => p.Id == id);
it returned the mocked item correctly.
Now, I am glad I figured it out, however, I am still scratching my head as to why that worked for unit testing, but the GetById was working as I was running the application.
Any insight would be most helpfull.
Thanks in advance.
Here is the mocked data:
public class AdministrationMockData
{
#region Administration Mock Data
#region County Mock Data
public static void CreateCounty(ref Mock<ICountyService> mock)
{
mock.Setup(m => m.GetAll()).Returns(new List<County>
{
new County { Id = 1, Name = "Adams", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 2, Name = "Berks", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 3, Name = "Chester", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 4, Name = "York", StateId = 39, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 5, Name = "Baltimore", StateId = 21, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new County { Id = 6, Name = "Montgomery", StateId = 21, IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" }
}.AsQueryable());
}
#endregion County Mock Data
#region State Mock Data
public static void CreateState(ref Mock<IStateService> mock)
{
mock.Setup(m => m.GetAll()).Returns(new List<State>
{
new State { Id = 21, Name = "Maryland", Code = "MD", IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" },
new State { Id = 39, Name = "Pennsylvania", Code = "PA", IsActive = true, LastChangedDate = DateTime.Now, LastChangedBy = "dba", AddedDate = DateTime.Now, AddedBy = "dba" }
}.AsQueryable());
}
#endregion State Mock Data
#endregion Administration Mock Data
}
Here is the Unit Test for Edit
[TestMethod]
public void Can_Edit_County()
{
// Arrange
// - create the mock repositories
Mock<ICountyService> mockCounty = new Mock<ICountyService>();
Mock<IStateService> mockState = new Mock<IStateService>();
AdministrationMockData.CreateCounty(ref mockCounty);
AdministrationMockData.CreateState(ref mockState);
// Arrange
// - create a controller and make the page size 6 items
CountyController controller = new CountyController(mockCounty.Object, mockState.Object);
controller.PageSize = 6;
controller.Testing = true;
// Act
County c1 = controller.Edit(1).ViewData.Model as County;
County c2 = controller.Edit(2).ViewData.Model as County;
County c3 = controller.Edit(3).ViewData.Model as County;
// Assert
Assert.AreEqual(1, c1.Id);
Assert.AreEqual(2, c2.Id);
Assert.AreEqual(3, c3.Id);
}
Here is the controller edit routine:
public ViewResult Edit(int id)
{
//County obj = _countyService.GetById(id);
County obj = _countyService.GetAll().FirstOrDefault(p => p.Id == id);
if (!Testing)
{
PopulateCountyDropDownLists(obj.StateId);
}
return View(obj);
}
I have a generic repository and interface repository which is instantiated by TEntity for each specific entity and also have a service and interface service to execute the repositories. My repository and service is based on: http://efmvc.codeplex.com/releases/view/58663
You get null because you never setup your CountyService to return proper data when calling GetById. Add the below code for setting up the mock and you'll be good to go with the GetById method:
mock.Setup(m => m.GetById(1).Returns(new State { Id = 1, ... });
or set it up to work with any argument passed to the mothod:
mock.Setup(m => m.GetById(It.IsAny<int>()).Returns(new State { ... });
Related
I have two tables with similar data for body insurance and third party car insurance ... I have used enum in the model to separate the insurances and I want to do the creation operation for it .... There are two modes for each insurance. One case when that car does not have insurance yet and the second case when we want to extend it.
I wrote this code to create the form, but it encounters the following error
I also get an error on the name of the Create function.error = not all code paths return a value.
Please advise
public async Task<IActionResult> Create(int id, int type)
{
InsuranceViewModel model;
ViewBag.Type = type;
var companies = await _context.InsuranceCompany
.Where(e => e.IsActice)
.ToListAsync();
ViewData["CompanyList"] = new SelectList(companies, "Id", "CompanyName");
if ((InsuranceType)type == InsuranceType.Body)
{
var bodyInsurance = await _context.BodyInsurance
.Include(e => e.InsuranceCompany)
.FirstOrDefaultAsync(e => e.Id == id);
if (bodyInsurance == null)
{
model = new InsuranceViewModel
{
CompanyId = bodyInsurance.InsuranceCompanyId,
CompanyName = bodyInsurance.InsuranceCompany.CompanyName,
InsuranceType = InsuranceType.Body,
IssueDate = new DateTime(bodyInsurance.IssueDate).Ticks,
ExpireDate = new DateTime(bodyInsurance.ExpireDate).Ticks,
VehicleInformationId = id
};
}
else
{
var lastBody = await _context.BodyInsurance.Include(e => e.InsuranceCompany)
.Where(e => e.VehicleInformationId == id)
.OrderBy(e => e.ExpireDate)
.LastAsync();
model = new InsuranceViewModel
{
ExpireDate = new DateTime(lastBody.ExpireDate).AddYears(1).AddDays(1).Ticks,
CompanyId = lastBody.InsuranceCompanyId,
CompanyName = lastBody.InsuranceCompany.CompanyName,
InsuranceType = InsuranceType.Body,
IssueDate = new DateTime(lastBody.ExpireDate).AddDays(1).Ticks,
VehicleInformationId = id
};
}
}
else
{
if ((InsuranceType)type == InsuranceType.Thirdpart)
{
var thirdParty = await _context.ThirdPartyInsurance
.Include(e => e.InsuranceCompany)
.FirstOrDefaultAsync(e => e.Id == id);
if (thirdParty == null)
{
model = new InsuranceViewModel
{
CompanyId = thirdParty.InsuranceCompanyId,
CompanyName = thirdParty.InsuranceCompany.CompanyName,
InsuranceType = InsuranceType.Body,
IssueDate = new DateTime(thirdParty.IssueDate).Ticks,
ExpireDate = new DateTime(thirdParty.ExpireDate).Ticks,
VehicleInformationId = id
};
}
else
{
var lastThirdParty = await _context.ThirdPartyInsurance.Include(e => e.InsuranceCompany)
.Where(e => e.VehicleInformationId == id)
.OrderBy(e => e.ExpireDate)
.LastAsync();
model = new InsuranceViewModel
{
ExpireDate = new DateTime(lastThirdParty.ExpireDate).AddYears(1).AddDays(1).Ticks,
CompanyId = lastThirdParty.InsuranceCompanyId,
CompanyName = lastThirdParty.InsuranceCompany.CompanyName,
InsuranceType = InsuranceType.Body,
IssueDate = new DateTime(lastThirdParty.ExpireDate).AddDays(1).Ticks,
VehicleInformationId = id
};
}
}
return View(model);
}
I have a list of users, each user has string array property called Tags. I am trying to get a unique list of tags and a total count, any idea what I a missing here? I am using LinqPad to write my test query, please see the example code:
void Main()
{
List<User> users = new List<User>(){
new User {Id = 1, Tags = new string[]{"tag1", "tag2"}},
new User {Id = 2, Tags = new string[]{"tag3", "tag7"}},
new User {Id = 3, Tags = new string[]{"tag7", "tag8"}},
new User {Id = 4, Tags = new string[]{"tag1", "tag4"}},
new User {Id = 5 },
};
var uniqueTags = users.Where(m=>m.Tags != null).GroupBy(m=>m.Tags).Select(m=> new{TagName = m.Key, Count = m.Count()});
uniqueTags.Dump();
// RESULT should BE:
// tag1 - Count(2)
// tag2 - Count(1)
// tag3 - Count(1)
// tag4 - Count(1)
// tag7 - Count(2)
// tag8 - Count(1)
}
public class User{
public int Id {get;set;}
public string[] Tags {get;set;}
}
You can flatten to IEnumerable<string> before grouping:
var uniqueTags = users.SelectMany(u => u.Tags ?? new string[0])
.GroupBy(t => t)
.Select(g => new { TagName = g.Key, Count = g.Count() } );
LINQPad C# Expression version:
new[] {
new { Id = 1, Tags = new[] { "tag1", "tag2" } },
new { Id = 2, Tags = new[] { "tag3", "tag7" } },
new { Id = 3, Tags = new[] { "tag7", "tag8" } },
new { Id = 4, Tags = new[] { "tag1", "tag4" } },
new { Id = 5, Tags = (string[])null }
}
.SelectMany(u => u.Tags ?? Enumerable.Empty<string>())
.GroupBy(t => t)
.Select(g => new { TagName = g.Key, Count = g.Count() } )
I am using .NET Web API and of course returning classes that are serialized to JSON. Up Until now I have not had to use the Data Contract attribute for any classes, but for this class below I do and I have no idea why. Intellitrace just says the class is unable to be serialized and to try adding a DataContract Attribute. I will but want to know why.
public class Card : BaseGridVM
{
private IEnumerable<Pc> _pcCards;
private IEnumerable<Pt> _ptCards;
private IEnumerable<MembershipCard> _membershipCards;
public Grid.Result Pt
{
get { return GetPtCardGrid(); }
}
public Grid.Result Pc
{
get { return GetPcCardGrid(); }
}
public Grid.Result Membership
{
get { return GetMembershipCardGrid(); }
}
public Card(IEnumerable<Pc> pcCards, IEnumerable<Pt> ptCards, IEnumerable<MembershipCard> membershipCards)
{
_pcCards = pcCards;
_ptCards = ptCards;
_membershipCards = membershipCards;
}
private Grid.Result GetPtCardGrid()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Card Name", width = 250},
new Grid.Header() {label = "Pts", width = 50},
new Grid.Header() {label = "Activation Date", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var card in _ptCards)
{
var row = new Grid.Row
{
id = card.id,
enabled = card.active.HasValue && (bool)card.active,
cell = new string[3]
};
row.cell[0] = card.cardName;
row.cell[1] = card.pts.HasValue ? card.pts.ToString() : "0";
row.cell[2] = card.activationDate.HasValue ? card.activationDate.ToString() : "-";
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
private Grid.Result GetPcCardGrid()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Offer Title", width = 200},
new Grid.Header() {label = "Pces Required", width = 70},
new Grid.Header() {label = "Activation Date", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var card in _pcCards)
{
var row = new Grid.Row
{
id = card.id,
enabled = card.active.HasValue && (bool)card.active,
cell = new string[3]
};
row.cell[0] = card.cardName;
row.cell[1] = card.pces.HasValue ? card.pces.ToString() : "0";
row.cell[2] = card.creationDate.HasValue ? card.creationDate.ToString() : "-";
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
private Grid.Result GetMembershipCardGrid()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Card Name", width = 200},
new Grid.Header() {label = "Members", width = 70}
};
var rows = new List<Grid.Row>();
foreach (var card in _membershipCards)
{
var row = new Grid.Row
{
id = card.id,
enabled = card.active.HasValue && (bool)card.active,
cell = new string[2]
};
row.cell[0] = card.cardName;
row.cell[1] = card.membersCount.HasValue ? card.membersCount.ToString() : "0";
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
}
This is Base GridVM
public abstract class BaseGridVM
{
protected static Grid.Result buildGrid(IEnumerable<Grid.Row> rows, IEnumerable<Grid.Header> headers, int page, int steps)
{
var row_array = rows.ToArray();
var result = new Grid.Result
{
rows = row_array,
page = page,
records = row_array.Count(),
steps = steps,
headers = headers.ToArray()
};
return result;
}
}
And this is another Class where a Data Contract is not requested
public class DashboardVM : IDashboardVM
{
public IResults_Dashboard results { get; private set; }
public Grid.Result topCompaniesGrid
{
get { return BuildTopCompanies(); }
}
public Grid.Result topAdsGrid
{
get { return BuildTopAds(); }
}
public DashboardVM(IResults_Dashboard results)
{
this.results = results;
}
private static Grid.Result buildGrid(IEnumerable<Grid.Row> rows, IEnumerable<Grid.Header> headers, int page, int steps)
{
var row_array = rows.ToArray();
var result = new Grid.Result
{
rows = row_array,
page = page,
records = row_array.Count(),
steps = steps,
headers = headers.ToArray()
};
return result;
}
private Grid.Result BuildTopCompanies()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Company Name", width = 150, click = true},
new Grid.Header() {label = "Coupon Views", width = 50, click = true},
new Grid.Header() {label = "Coupon Clicks", width = 50},
new Grid.Header() {label = "Coupon Redemptions", width = 50},
new Grid.Header() {label = "Ad Views", width = 50, click = true},
new Grid.Header() {label = "Ad Clicks", width = 50},
new Grid.Header() {label = "Reward Cards", width = 50},
new Grid.Header() {label = "Fees", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var company in results.companies)
{
var row = new Grid.Row {id = Convert.ToInt32(company.companyId), cell = new string[8]};
row.cell[0] = company.companyName;
row.cell[1] = company.couponViews.ToString();
row.cell[2] = company.couponClicks.ToString();
row.cell[3] = company.couponRedemptions.ToString();
row.cell[4] = company.adViews.ToString();
row.cell[5] = company.adClicks.ToString();
row.cell[6] = company.rewardCards.ToString();
row.cell[7] = company.revenue.ToString();
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
private Grid.Result BuildTopAds()
{
var headers = new List<Grid.Header>
{
new Grid.Header() {label = "Company Name", width = 150, click = true},
new Grid.Header() {label = "Ad Name", width = 150, click = true},
new Grid.Header() {label = "Views", width = 50},
new Grid.Header() {label = "Clicks", width = 50},
new Grid.Header() {label = "Fees", width = 50}
};
var rows = new List<Grid.Row>();
foreach (var ad in results.ads)
{
var row = new Grid.Row {id = Convert.ToInt32(ad.Id), cell = new string[5]};
row.cell[0] = ad.companyName;
row.cell[1] = ad.name;
row.cell[2] = ad.views.ToString();
row.cell[3] = ad.clicks.ToString();
row.cell[4] = ad.fees.ToString();
rows.Add(row);
}
return buildGrid(rows, headers, 1, 3);
}
}
I solved this problem without the Data Contract by adding a parameterless constructor and adding set methods to the properties.
What I gathered from this is that the serializer creates a new object using the parameterless constructor and then copies the public values over. I say this because when I did not add the set methods to the properties it gave me an empty object. Once I added the set methods the values were returned within the object.
I think it could be a little more sophisticated. I don't know why it creates another object vs using the one I gave, but that's another topic!
I have a controller method that acceses a repository method which has lambda expression as parameter:
// GET: /Product/
public ViewResult List(string category, int page = 1) {
ProductsListViewModel productsListViewModel = new ProductsListViewModel {
Products = _repository.GetByPage(q => q.Category == category || category == null, page, PageSize),
PagingInfo = new PagingInfo {
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = _repository.Get(q=>q.Category==category || category==null).Count()
},
CurrentCategory = category
};
return View(productsListViewModel);
}
In my unit test, when controller invokes the repository method, returned object("result" variable) is always null, do you have any idea about this situation?
public void Can_Paginate() {
//Arrange
//Create mock repository
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(q => q.GetByPage(c=>c.Category=="C1",1,3)).Returns(new List<Product>
{
new Product {Id = 1, Name = "P1", Category = "C1"},
new Product {Id = 2, Name = "P2", Category = "C1"},
new Product {Id = 3, Name = "P3", Category = "C1"}
});
mock.Setup(q => q.Get(c => c.Category == "C1")).Returns(new List<Product>
{
new Product {Id = 1, Name = "P1", Category = "C1"},
new Product {Id = 2, Name = "P2", Category = "C1"},
new Product {Id = 3, Name = "P3", Category = "C1"}
});
//Create a controller and make page size 3 items
ProductController controller = new ProductController(mock.Object);
controller.PageSize = 3;
//Action
ProductsListViewModel result = (ProductsListViewModel) controller.List("C1", 1).Model;
//Assert
Assert.IsTrue(result.Products.Count()==3);
}
Thanks
Just had quick look at the moq wiki and it had an example of matching Func<int>
as a parameter. So to match an argument of Func<string> you would write something like this:
mock.Setup(q => q.GetByPage(It.Is<string>(c=>c.Category=="C1"),1,3)).Returns...
I am initializing the entities like this:
TasksListDB db = new TasksListDB();
public ActionResult Index()
{
var tasks1 = new List<Task>()
{
new Task { Id=1, Name = "Task 1", Difficulty = 1, DateCreated = DateTime.Parse("1/12/2011") , IsDone= true },
new Task { Id=2, Name = "Task 2", Difficulty = 2, DateCreated = DateTime.Parse("11/2/2011") , IsDone = false}
};
var tasks2 = new List<Task>()
{
new Task { Id=3, Name = "Task 3", Difficulty = 3, DateCreated = DateTime.Parse("11/2/2011") , IsDone = false},
new Task { Id=4, Name = "Task 4", Difficulty = 5, DateCreated = DateTime.Parse("1/2/2010") , IsDone= true }
};
tasks1.ForEach(t => db.Tasks.Add(t));
tasks2.ForEach(t => db.Tasks.Add(t));
var Persons = new List<Person> {
new Person { Id= 1 , Age= 10, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed" },
new Person { Id= 2 , Age= 10, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed" },
new Person { Id= 3 , Age= 10, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed" },
new Person { Id= 4 , Age= 10, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed" }};
Persons.ForEach(p => db.Persons.Add(p));
return View(db.Tasks);
}
my db context looks like this:
public class TasksListDB : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Task> Tasks { get; set; }
}
I want to assign tasks to persons how can I do this ?
My model looks like this :
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string EmailAddress { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
}
public class Task
{
public int Id { get; set; }
public string Name { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]
[Range(1,5,ErrorMessage="Difficulty must be between 1 and 5")]
public int Difficulty { get; set; }
public bool IsDone { get; set; }
public virtual Person Person { get; set; }
}
When I run my mvc application, Person and Task tables are created but juntion table PersonTask not created.
[EDIT]
I am assigning tasks like this in initializer class:
var t1 = new Task { Id = 1, Name = "Urgent Task", DateCreated = DateTime.Now, Difficulty = 2, IsDone = true };
var t2 = new Task { Id = 2, Name = "Business Task", DateCreated = DateTime.Now, Difficulty = 1, IsDone = true };
var t3 = new Task { Id = 3, Name = "Home Task", DateCreated = DateTime.Now, Difficulty = 2, IsDone = false };
context.Tasks.Add(t1);
context.Tasks.Add(t2);
context.Tasks.Add(t3);
var Persons = new List<Person> {
new Person { Id= 1 , Age= 40, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed", Tasks= new List<Task> { t1,t2 }},
new Person { Id= 2 , Age= 30, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t1} },
new Person { Id= 3 , Age= 29, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t3,t2 } },
new Person { Id= 4 , Age= 35, EmailAddress= "asif_hameed_37#hotmail.com", FirstName= "Asif", LastName="Hameed" , Tasks= new List<Task> { t1,t3 } }};
context.Persons.Add(Persons[0]);
context.Persons.Add(Persons[1]);
context.Persons.Add(Persons[2]);
context.Persons.Add(Persons[3]);
context.SaveChanges();
but when i see the task table in sql server, it looks like this:
Id Name DateCreated Difficulty IsDone Person_Id
1 Urgent Task 2011-04-24 19:32:03.990 2 1 4
2 Business Task 2011-04-24 19:32:03.990 1 1 3
3 Home Task 2011-04-24 19:32:03.990 2 0 4
I assume that person table should have tasks not the task table should have person Id
Please suggest solution.
Junction table will not be created because you have defined one-to-many relation not many-to-many. The Task entity in your model can be assigned only to single Person not to many persons.
If you want to assing Task to Person simply run:
person.Tasks.Add(task);