Jakie testy można by napisać dla tych kontrolerów.Testować interfejsu się nie da ,a u mnie same repozytoria.
public class HomeController : Controller
{
private IMainCategoryByCitiesRepository repository;
private IIntermediateCategoryRepository repositoryIntermediate;
public HomeController(IMainCategoryByCitiesRepository repository, IIntermediateCategoryRepository repositoryIntermediate)
{
this.repository = repository;
this.repositoryIntermediate = repositoryIntermediate;
}
public ActionResult Index()
{
return View();
}
public ViewResult List()
{
return View(repository.MainCategoryByCities);
dla tej mam test
[TestFixture]
class ForumTests
{
[Test]
public void List_Test()
{
Mock<IMainCategoryByCitiesRepository> mock = new Mock<IMainCategoryByCitiesRepository>();
mock.Setup(m => m.MainCategoryByCities).Returns(new MainCategoryByCities[]
{
new MainCategoryByCities(){ MainCategoryByCitiesId=1,CityName="Aleksandrów" },
new MainCategoryByCities(){ MainCategoryByCitiesId=2,CityName="Bydgoszcz" },
new MainCategoryByCities(){ MainCategoryByCitiesId=3,CityName="Chełmno" },
new MainCategoryByCities(){ MainCategoryByCitiesId=4,CityName="Grudziądz" },
new MainCategoryByCities(){ MainCategoryByCitiesId=5,CityName="Świecie" }
});
Mock<IIntermediateCategoryRepository> mock2 = new Mock<IIntermediateCategoryRepository>();
HomeController controller = new HomeController(mock.Object, mock2.Object);
IEnumerable<MainCategoryByCities> result = (IEnumerable<MainCategoryByCities>)controller.List().Model;
MainCategoryByCities[] mainCategoryArray = result.ToArray();
Assert.IsTrue(mainCategoryArray.Length == 5);
Assert.AreEqual(mainCategoryArray[0].CityName, "Aleksandrów");
Assert.AreEqual(mainCategoryArray[2].CityName, "Chełmno");
Assert.AreEqual(mainCategoryArray[4].MainCategoryByCitiesId, 5);
}
}
public ViewResult Show_IntermediateCategory_List(int id)
{
List<IntermediateCategory> list = new List<IntermediateCategory>();
list = repositoryIntermediate.GetIntermediateCategory_ById(id).ToList();
return View(list);
}
}
public class TopicController : Controller
{
ITopicRepository repository;
public TopicController(ITopicRepository repository)
{
this.repository = repository;
}
public ViewResult Show_Topics(int id)
{
ViewBag.IntermediateCategory_Id = id;
List<Topic> TopicList = repository.Get_Topics_ByIntermediateCategory(id).ToList();
return View(TopicList);
}
[Authorize]
public ViewResult Add_New_Topic(int id)
{
ViewBag.InterId = id;
return View(new Topic());
}
[HttpPost]
public RedirectToRouteResult Add_New_Topic(Topic topic)
{
string UserId= User.Identity.GetUserId();
repository.Add_New_Topic_To_Database(topic,UserId);
return RedirectToAction("Show_Topics",new {controller="Topic",action="Show_Topics",id=topic.IntermediateCategoryId });
}
public ActionResult Go_To_Topic(int id)
{
TopicViewModel viewModel = repository.Get_Topic_ViewModel(id);
return View(viewModel);
}
[Authorize]
public ActionResult Add_New_Comment(int id)
{
Topic topic = repository.Get_Topic_By_Id(id);
AddingCommentViewModel adding = new AddingCommentViewModel();
adding.topic = topic;
return View(adding);
}
[HttpPost]
[Authorize]
public RedirectToRouteResult Add_New_Comment(Comment comment)
{
string UserId = User.Identity.GetUserId();
repository.Add_New_Comment_To_Topic(comment,UserId);
return RedirectToAction("Go_To_Topic", new { controller = "Topic", action = "Go_To_Topic", id = comment.TopicID });
}
Interfejs
namespace ForumProject.Repository
{
public interface ITopicRepository
{
IEnumerable<Topic> Topics { get; }
IEnumerable<Topic> Get_Topics_ByIntermediateCategory(int id);
MainCategoryByCities Get_MainCategoryByCities_To_Add(int id);
bool Add_New_Topic_To_Database(Topic topic,string UserId);
TopicViewModel Get_Topic_ViewModel(int id);
bool Add_New_Comment_To_Topic(Comment comment,string UserId);
Topic Get_Topic_By_Id(int id);
}
public interface IMainCategoryByCitiesRepository
{
IEnumerable<MainCategoryByCities> MainCategoryByCities { get; }
}
public interface IIntermediateCategoryRepository
{
IEnumerable<IntermediateCategory> IntermediateCategory { get; }
IEnumerable<IntermediateCategory> GetIntermediateCategory_ById(int id);
}
}