Exercise 2: Testing CRUD actions

In this exercise, you will learn how to test CRUD actions that modify the database. One of the premises to take into account is that tests don’t necessarily run in a specific order. Another one is that every test could be executed as many times as needed. So your tests should not depend on previous tests executions. For instance, if your test is going to delete a record in the database, the setup of the test should provide the record to be deleted. This way, the test could be executed repeatedly.

Task 1 – Reviewing the provided solution

Some changes where performed to the end solution of the last exercise in order to prepare it for this one.

  • A test project is provided to hold the tests for this exercise
  • The CRUD tests were created using the approach followed in the previous exercise (right-click on the class to be tested and then Create Unit Tests)
  • The test project has its own database for testing purposes: since in this exercise you are testing actions that rely on a database, a well-known database for testing purposes was included inside the testing project. This database includes the tables used by the entity classes and has data pre-loaded to simplify test execution.

    Figure 1

    Well-known database as part of the testing project

  • A web.config was added to the project to connect the solution to this database in order to avoid modifying data in the project to be tested.

In this task, you will review those changes before moving on, in the following tasks, with the particular additions for testing CRUD actions.

  1. Start Microsoft Visual Web Developer 2010 Express from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
  2. In the File menu, choose Open Project. In the Open Project dialog, browse to Source\Ex02-Testing CRUD actions\Begin, select MvcMusicStore.sln and click Open. Based on the changes explained, you should use this solution instead of the one you ended exercise 1 with.
  3. In the Test menu, select Run and then All Tests in Solution.

    The tests are executed and fail. In this exercise, you will modify these tests to make them succeed.

    Figure 2

    Running all the tests

Task 2 – Adding transaction capabilities to the Test Methods

In this task, you will learn how to use transactions to limit the scope of the test. This way, you could execute the test as many times as you want, without altering the database content.

  1. In the Solution Explorer right-click the project MvcMusicStore.Tests and select Add Reference.

    Figure 3

    Adding a Reference

  2. In the Add Reference window select the Net tab and locate the System.Transactions component and click OK.

    Figure 4

    Adding System.Transactions reference

    Note:
    The System.Transactions classes provide explicit and implicit transaction programming model. In the implicit model, when you use TransactionScope the transaction manager determines if it will subscribe to a current transaction or if it will instantiate a new one. This depends on two factors: whether an ambient transaction already exists and also the TransactionScopeOption parameter in the constructor.

    When you use TransactionScope in a using block, the whole block is transactional. This avoids you from having to focus on the transaction scope. If you were using a SQL Server database (not a local one, as now) and you wanted to commit the changes to the database, you would have toinvoke TransactionScope’s Complete method. Since the intention in this exercise is to maintain the database intact for the next test, you will not see that invocation in the code. Nevertheless you should remember to use it appropriately when your intention is to commit the changes of the transaction.

  3. Add the following using directives in the StoreManagerControllerTest.[cs|vb] class. To do this, add the following references before the namespace definition:

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 Using directives – CSharp)

    C#

    using MvcMusicStore.Controllers;
    FakePre-86384227c4d84c61b9e822e82ed897be-576d3e40c5154f55a746d981ed08742dFakePre-de00b90a58f84e75913d87d43b4fed38-a280db47107942c79c04d93e0fd5f47bFakePre-e1b568d118114e65bfdd77b4c2c0cc28-7b7a3e30d4984e5791920cdef9592f4bFakePre-04407e86d18d48c89c09636ee4b2bfa2-ba850d86bf0b4e8a8af7c332f3bd285eFakePre-a5491b40856143d299d5c7c4f6767621-5a4efd46c1b946f3b443271e67cb4d9fusing System.Transactions; using System.Linq; using MvcMusicStore.ViewModels;FakePre-3a7b92ff509f4dcdbcaca8cb4d26d336-ca06ae8e9c0e4b9fb48ceaff7d2279beFakePre-04504fb0db9f4b5099dd19a143ef8ec5-0a5918936d9249e5953e016b8a0ab84fFakePre-cc10e91377494e3bbed5b9c16cb1bcfa-b43929605f944b5792861a4b6c7156c9

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 Using directives – VB)

    Visual Basic

    Imports MvcMusicStore.Controllers
    FakePre-21fff37c651b4b318f854a524ac42e6b-80bc71f0516d48f7bdf92781dd50a10cFakePre-2d765dbc659b4ae4812e425c1c7791e0-753ea1d6644343b882a65b05d6fc14f8FakePre-a3341059306a44109e33d3a128f99df3-9510b953305f4cdfa0c5c542e38fa229FakePre-a8b1c6a196de451f82961d122f84cb41-e6534608adbb44cfabd7e3d73b726074FakePre-72c3f6c9bf544094b91354e9bfdcc667-afb056f543ce4e7ebb954169612fdab8Imports System.Transactions Imports System.Linq Imports MvcMusicStore.ViewModelsFakePre-cb213b711bd14999a245856b29352b3e-bff8ffb1fa234abbab2c75e23a105b36FakePre-2fb8f5362a1843b6b0ba154fff3f83aa-b43e148c24f04a05b164343a50ba5359

Task 2 – Testing Create Action Method

In this task, you will learn how to test the creation of new records in the database by adding new albums.

  1. Open StoreManagerControllerTest.[cs|vb] class and locate the CreateTest method.
  2. Test the addition of a new Album, then retrieve it from the database and compare its attributes to verify that it was correctly persisted. To do this, replace the CreateTest method with the following code.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 CreateTestMethod – CSharp)

    C#

    [TestMethod()]
    FakePre-c75b2e11dc80413f81b7eed8f3789eb5-cac947823013443282240629b82b9cf0FakePre-f98cfcacc0644d249d41f49bd6442d7c-25df010012a44bc5a0ff08f510295630FakePre-793450c87d374323a4fcab9fcb2d133d-0439a1cd89b44e12b531e42b91faf365FakePre-22c66b46c3044cf6915e50a64781bbbc-3922d5972dd0426ba88a2e12ef480163 using (TransactionScope ts = new TransactionScope()) { StoreManagerController target = new StoreManagerController(); Album album = new Album() { GenreId = 1, ArtistId = 1, Title = "New Album", Price = 10, AlbumArtUrl = "/Content/Images/placeholder.gif" }; ActionResult actual; actual = target.Create(album); Assert.IsTrue(album.AlbumId != 0); MusicStoreEntities storeDB = new MusicStoreEntities(); var newAlbum = storeDB.Albums.SingleOrDefault(a => a.AlbumId == album.AlbumId); Assert.AreEqual(album.GenreId, newAlbum.GenreId); Assert.AreEqual(album.ArtistId, newAlbum.ArtistId); Assert.AreEqual(album.Title, newAlbum.Title); Assert.AreEqual(album.Price, newAlbum.Price); Assert.AreEqual(album.AlbumArtUrl, newAlbum.AlbumArtUrl); }FakePre-3b843edcd29c44b88a68d97c0169678a-b1554c032ddd454198e47b33c6ba31b7FakePre-aa084361603f483db6f0e1263973d4f0-4ec089fe963e41619dfa321e3c2c1a52FakePre-6017d8612e144650b51cce27359fcaf2-ca4641c79b2148b3a035618d47fcc811FakePre-7a1fee90642743479393a1cd15ff5212-cf309c56360f49c7898ea96b35a9e4c1FakePre-d41d6fb0a8db408ea6c77bc710ce3acf-0ef2952beeaf4ff3938f204df56a7df7

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 CreateTestMethod – VB)

    Visual Basic

    <TestMethod(), DeploymentItem("MvcMusicStore.mdf"), DeploymentItem("MvcMusicStore_log.ldf")>
    FakePre-860459d867814464b032c1f5ca454944-257d2273fdc1441db5758e910352a7e9 using (TransactionScope ts = New TransactionScope()) Dim target As New StoreManagerController() Dim album As New Album() With {.GenreId = 1, .ArtistId = 1, .Title = "New Album", .Price = 10, .AlbumArtUrl = "/Content/Images/placeholder.gif"} Dim actual As ActionResult actual = target.Create(album) Assert.IsTrue(album.AlbumId <> 0) Dim storeDB As New MusicStoreEntities() Dim newAlbum = storeDB.Albums.SingleOrDefault(Function(a) a.AlbumId = album.AlbumId) Assert.AreEqual(album.GenreId, newAlbum.GenreId) Assert.AreEqual(album.ArtistId, newAlbum.ArtistId) Assert.AreEqual(album.Title, newAlbum.Title) Assert.AreEqual(album.Price, newAlbum.Price) Assert.AreEqual(album.AlbumArtUrl, newAlbum.AlbumArtUrl)FakePre-7416a7c31cbf4f759fce6d329c9dff36-326e2cce262f471991e43f234b2d9852FakePre-6171a358e37b473b9880207b85d2eb96-e1c70e8d461d40d6b95411b6695e8c5bFakePre-c91d44fe6bf9451dbbfade28c28b6761-01cdfc075f0b43d4bafbcab78ae43326FakePre-4fc5e07f97ec4f8aa4f043ca6b3bee24-8fa52766e742441392e4d96401f7b611FakePre-b2b279a0fb2748d3a8d44f65704836b8-59d2f457da3e48419dd6a1de1eeeccf6

    Note:
    You will notice that before the test method two new DeploymentItem attributes were added. This two attributes refer to the database and its log file.

    The DeploymentItem attribute is used in test methods to alert to the test engine of files that will be needed to execute tests. Visual Studio run tests in a different folder where it copies the built assemblies. This folder is created for each test execution and when a test is run, code-under-test assemblies, and the deployment items are placed in a test deployment folder specific for that test run. Since these files are copied every time the test is executed, the database used for the test execution always starts with the same set of data.

  3. Run the test. On the StoreManagerControllerTest.[cs|vb] class right-click the CreateTest method and select Run Tests

    Figure 5

    Running the CreateTest test

Task 3 – Testing Delete Action Method

In this task, you will learn how to test the deletion of albums.

  1. Open StoreManagerControllerTest.[cs|vb] class and locate the DeleteTest method.
  2. Test the deletion of an Album and then verify it no longer exists in the database. To do this, replace the DeleteTest method with the following code.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 DeleteTestMethod – CSharp)

    C#

    /// <summary>
    FakePre-5c9e372518c343bd985bf7d137507893-d007e0a88fdd41229800b640146048e6FakePre-04807ae911774c41a08599e6402e8964-640b3a6929404c95a968e2e1b2db39bcFakePre-d823c1697d4144cca0befd5a2655a4f5-c87a37514401458282eed006c1dce83dFakePre-025542f252314bc6ac7ecc3398989115-efcf02c390a74b589677bd54c68c106aFakePre-3a953298a452420e987a3b566767ac51-ec185ba1c6844952ade78c843947943fFakePre-7526e56d7a4d4b4eaaacdbb5bd65b364-51c6d9a2c99a4562b87fbc7ee5848cebFakePre-5fe844f9482949cd9a3b5062b00b0619-79e22fbc46f34961b1f5240a17de80bd using (TransactionScope ts = new TransactionScope()) { StoreManagerController target = new StoreManagerController(); int id = 669; ActionResult actual; actual = target.Delete(id, null); Assert.IsNotNull(actual); MusicStoreEntities storeDB = new MusicStoreEntities(); var album = storeDB.Albums.SingleOrDefault(a => a.AlbumId == id); Assert.IsNull(album); }FakePre-a2494ec156ef4db48c28cd75f7d8aab1-ba70bbf40f0b475bbdce7a9d8eadbad9FakePre-a945d1917dbb4de2bec9310680060b2d-e04e4857a12145db955bfbcd277fcc48FakePre-166ee629316249c89536c5e2df1213ed-de1ebcf307174d648400a46da9a851a6FakePre-4b98f1125e7240b9981d7566cc9f6c32-c9b71189871e466097be3b9343e2111a

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 DeleteTestMethod – VB)

    Visual Basic

    ''' <summary>
    FakePre-fc13c28636dd45bc94a6fabc5ff3b070-3740cbe5e46144b08e8b29a7d9b36e73FakePre-f88debe4c6e74e28a36b10a86620af6b-c20f875cbf7d42b9a9819e587e417738FakePre-bbfcf233c6764d31838745e201a0bf8b-eb933ccce9bb463192401f7ed9516909FakePre-93a4c34bd3374f118511538a6aad9350-06fa1b765f6e468f9ef1c4a08ae11ae5 using (TransactionScope ts = New TransactionScope()) Dim target As New StoreManagerController() Dim id As Integer = 669 Dim actual As ActionResult actual = target.Delete(id, Nothing) Assert.IsNotNull(actual) Dim storeDB As New MusicStoreEntities() Dim album = storeDB.Albums.SingleOrDefault(Function(a) a.AlbumId = id) Assert.IsNull(album)FakePre-5191a4fc0119423c8b5f6c7efcef0f07-b4459e31cd2c427bab4c1b109176aae4FakePre-4fd6e68873d745f19b7b288a68cbf593-2c74403e23d34a35870d50a07cd9de2dFakePre-94050b572e0e4063bfd5eb458592b625-a96cd6bd23b946018e35a94cc4c4e42bFakePre-607852407a7e4dca81574898a91a32a1-544480e03e8340e08834102bc4ae9623

  3. Run the test. On the StoreManagerControllerTest.[cs|vb] class right-click the DeleteTest method and select Run Tests

    Figure 6

    Running the DeleteTest test

Task 4 – Testing Edit Action Method

In this task, you will learn how to test the HTTP-GET edit action method.

  1. Open StoreManagerControllerTest.[cs|vb] class and locate the EditTest method.
  2. Test the edit action by obtaining an Album’s information. To do this, replace the EditTest method with the following code.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 EditTestMethod – CSharp)

    C#

    /// <summary>
    FakePre-2ce6872938ae489ebb7dfa14b03ee2b7-1cd8067766a44873afb9ccf12679f8d6FakePre-6a271dbfa59f4ec5ab04c52416e53824-05d7121df24a4748b4b1e620784bd9ccFakePre-a0fa91d68b6a4363b397dc74f015602f-7aeae2c8f7894567a8657a8d1f12b98aFakePre-4187f7f9f0414943a9ab4fe6df714d63-b70aaf0fa1ec48868eee3700c9235e52FakePre-d0e163f1abe74dc892fb17f64affc878-6ca11f663e6b41a0bebed573b0dd3934FakePre-087a69547a4f41f689ca89aabe9d8cdf-4f02f62ad871456ea527ebcc7b54593dFakePre-6e7dae923d8146c1b6185887dbca3037-4e4e04853ba744b2bc63aaaefe702147 StoreManagerController target = new StoreManagerController(); int id = 669; ActionResult actual; actual = target.Edit(id); Assert.IsNotNull(actual); Assert.IsInstanceOfType(actual, typeof(ViewResult)); ViewResult viewResult = (ViewResult)actual; Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(StoreManagerViewModel)); StoreManagerViewModel model = (StoreManagerViewModel)viewResult.ViewData.Model; Assert.AreEqual(id, model.Album.AlbumId); Assert.AreEqual("Ring My Bell", model.Album.Title); Assert.AreEqual(10, model.Genres.Count());FakePre-28c4de11392f4b91b04b1c11d0a22618-2dc47f6b2a2b4ff2a12feda653bf30a4FakePre-65966b611e6240a094523cc5979e6477-3a01f8bebe124381aaf1a064a1d91183FakePre-b38c76db000d4cdbb71fb497c057d040-9d024c82b1284f85859ab0cfbc8936bbFakePre-01489dc6a1a9483999b50a9ee4958a36-7c7d54e146b94433b01bd59c7469ca5aFakePre-e69d189625d1455a85ad1665f661e90f-5be2d29702b04d379c5910cb55f2a5d3FakePre-aa43fa028b1d4da88bf66ed70fd16bbb-976725a5efc044e397d6905fdfe50b32

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 EditTestMethod – VB)

    Visual Basic

    ''' <summary>
    FakePre-6efbb2db7b194e2d8acf1b8db87cfc0b-9df9586f8f2e455aafcdde66bc1110f8FakePre-9e0020d5e6cd4559ad15620159a84162-89cdab86484049f2863134ed0fbb59ddFakePre-a708fada6ffa45929d0c7b60c6352964-cfb76f0ed1ed488fb0df3d0a119112d4FakePre-891d611c9b294f99ac494dc8e88c8571-04043cee31ee4927ba3048d92e2e329a Dim target As New StoreManagerController() Dim id As Integer = 669 Dim actual As ActionResult actual = target.Edit(id) Assert.IsNotNull(actual) Assert.IsInstanceOfType(actual, GetType(ViewResult)) Dim viewResult As ViewResult = CType(actual, ViewResult) Assert.IsInstanceOfType(viewResult.ViewData.Model, GetType(StoreManagerViewModel)) Dim model As StoreManagerViewModel = CType(viewResult.ViewData.Model, StoreManagerViewModel) Assert.AreEqual(id, model.Album.AlbumId) Assert.AreEqual("Ring My Bell",model.Album.Title) Assert.AreEqual(10, model.Genres.Count())FakePre-f125a5f5f0514704bb3bdeb76eb37f1e-7ce5327b190f4adab59cbe67a7b444c3FakePre-a2b8898eb74c42b584c603a898b487d7-22aafa80f97a4001b2506d130cb90dddFakePre-3d76673637e74525b905bbf4977ded95-fb9f5fa07dfb45cf87bd9e811d2c4d2bFakePre-3a5bab84367b4f338fcb41f5659e1fbb-127fb696d9454bf38d3936a64a290ae9FakePre-e94542477adf40e8a97e0c66ef7739a0-054f2c01ac0c4d5fbf78ed76d4afdd07

    Note:
    In this code block you first verify that the Edit action method returns an ActionResult object and that is of type ViewResult. This conversion is necessary to reach the model defined in the StoreManagerViewModel. Finally, the album obtained is compared by its ID and Title attributes.

  3. Run the test. On the StoreManagerControllerTest.[cs|vb] class right-click the EditTest method and select Run Tests

    Figure 7

    Running the EditTest test

Task 5 – Testing Index Action Method

In this task, you will test the Index action method that obtains the list of genres from the store.

  1. In the Solution Explorer expand the project MvcMusicStore.Tests and double click on the StoreControllerTest.[cs|vb] class.
  2. Add the following namespaces at the beginning of the StoreControllerTest.[cs|vb] file:

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 StoreControllerTestUsing – CSharp)

    C#

    using MvcMusicStore.Controllers;
    FakePre-df28ba9962c54972968e694e81d641d9-fe1934f0eaa84e898afc94c7c388b02dFakePre-83b093a34ad74f69b55bf09a2067438c-6ea2efc2167e4656bb6d5204c78b5c83FakePre-2028df32f2c14d81a47f65a6286f69f0-cf6f72d4bd7b41efa41d629aef1c4003FakePre-dea02d15d7524bab8e79b2f6251bcf14-af0bb02c83d2494c9a7f7b12b0a4dc85using MvcMusicStore.ViewModels; using MvcMusicStore.Models;FakePre-d81a21afe664421383b09d02df758c3f-429c0324101d4dde8853810209df20fdFakePre-f07636c88cef4388b7fdb103034dc3cf-8f15ae5ca65e4325b437eeec09a65082FakePre-0ed8a853e8af429a98ed978d72b8b240-acd7131eb9494c9287266a5bc21d81dc

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 StoreControllerTestUsing – VB)

    Visual Basic

    Imports MvcMusicStore.Controllers
    FakePre-6761df147450478c9ad58fb5f219bfe2-25e1e50a4ae442bfa25372e3214961f4FakePre-5f88682f08d44864a595a707522645a8-b7b7dfe0465646f480482b5b6f941c91FakePre-d99fcdb5d3ed48f7945af0263d438eb1-d746d65906a1434cb4d5324a1a8a7118FakePre-cfffae04af0249c8af8710e52484c6a5-020cb9c7592b4305a492e64b3e753cfeImports MvcMusicStore.ViewModels Imports MvcMusicStore.ModelsFakePre-c787b3a0af99428ebb1dbd79c0ad5423-99e91393784f422e8a4072eb44e54ff3FakePre-e892217c2d614c4a9f3c306985d6120f-1a9075faaa424207a434a3076c8de47f

  3. Test getting all the Genres for the Music Store by invoking the Index action method and verify the amount of genres retrieved. To do this, replace the IndexTest method with the following code.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 IndexTestMethod – CSharp)

    C#

    /// <summary>
    FakePre-8e5c19d6a5d94e3a9d8050ea048de8d3-fa3eb62b357542619678c071184e61ddFakePre-8429e559f2e64c96bef660b1e1f478fb-9e5284c3b9734e0dba82f201064fa0f6FakePre-2170ddb012884a48b0ff6193d69cc9a1-70484214489b4d5c94439ff5e6c81b9aFakePre-eaa8d699d0c44f7abfbd37c8a08ddba3-9f58f6bc58784f86840f45945e46fe9fFakePre-da024d324a6b42ffb116dd0697779446-671013630cef455fbcc39bcc21cb74c8FakePre-0802686ade994bd7be6993b075942b31-849e97f53f544fd9b8cbacfecf0bffc5FakePre-05d0fcfe0ba546538857ed52897b1df1-7193d1ce9b4c41b2931325ecdf07ac24 StoreController target = new StoreController(); ActionResult actual; actual = target.Index(); Assert.IsNotNull(actual); Assert.IsInstanceOfType(actual, typeof(ViewResult)); ViewResult viewResult = (ViewResult)actual; Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(StoreIndexViewModel)); StoreIndexViewModel model = (StoreIndexViewModel)viewResult.ViewData.Model; Assert.AreEqual(10, model.Genres.Count); Assert.AreEqual(10, model.NumberOfGenres);FakePre-f4ddc61d95dd45e0ad34658afb2b343b-6ba7e928df8448d3ad93cc9182371f95FakePre-f1780a78b14d45d5b408ac1a11e0fd56-03f901b274df4a9b8b385f24a90254dcFakePre-a0b0e8918be34da6baf6394459f318b4-8fc18f130b214f15a97d4a08e8c89ef3FakePre-d59fd46ecea7402791ebdbeeb07f49ce-5e55a51c166442ddbace844d6db2b9ffFakePre-1cedb83ec0f449daa63a31e3d3739740-02b9c149dc0d4b69ac88b8263aa9f59f

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 IndexTestMethod – VB)

    Visual Basic

    ''' <summary>
    FakePre-29d58f1e40bc49b0a0cab6e9c4f8da2b-d55dc3bb69394d28acae48547465503dFakePre-56bb22cca8df442a9a40b03480ff0a31-cc8b4618993a43198d3ea06e4655d66aFakePre-4c32dbd73d554abdaaef09a22cd2720e-c3803e19f9ff4b5b8bb580ff29927b75FakePre-f0ba1be8fea248fdad5a1c484150140c-a847c50b3d244ea1afd35f22ac2e949fDim target As New StoreController() Dim actual As ActionResult actual = target.Index() Assert.IsNotNull(actual) Assert.IsInstanceOfType(actual, GetType(ViewResult)) Dim viewResult As ViewResult = CType(actual, ViewResult) Assert.IsInstanceOfType(viewResult.ViewData.Model, GetType(StoreIndexViewModel)) Dim model As StoreIndexViewModel = CType(viewResult.ViewData.Model, StoreIndexViewModel) Assert.AreEqual(10, model.Genres.Count) Assert.AreEqual(10, model.NumberOfGenres)FakePre-61c9b152ca974c81ad3e144762a6e7a4-40739eb9629949ec8909e79a7acb43f2FakePre-ba5d62a78b544db6a8fb0de4605ad6b1-e26d16a6b78540b1ad8efb356bb9c291FakePre-952ce1e51f724d6ea7b0dd90be5e93fa-415fbd4af0bd46a9b09690a57e7097afFakePre-33af464efa64451499bcfecfd646b05e-9261fff38d334bb8a93ff77d55594df0FakePre-bd9d6020140f4de395f7d17435851b93-cad23067738a4ec8b4211ae9a04fa188

  4. Run the test. On the StoreControllerTest.[cs|vb] class right-click the IndexTest method and select Run Tests.

    Figure 8

    Running the IndexTest test

Task 6 – Testing Details Action Method

In this task, you will test the Details method. This method obtains a specific album based on its ID.

  1. Open StoreControllerTest.[cs|vb] class and locate the DetailsTest method.
  2. Test getting the Album’s information for a record stored in the provided database. To do this, replace the DetailsTest method with the following code.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 DetailsTestMethod – CSharp)

    C#

    /// <summary>
    FakePre-4037ac53246b4816a74424ec8bba912f-01da7bb9241f4cdf973f4da4e045068dFakePre-e991b84a292f4c64b5cae0afe464185a-17a37d1cc87445ca910d5e3e458bf5b1FakePre-d197d3bb9b734b768752058b2c368aa5-591840af241f4ce1b7ce6aef5a6cd0b7FakePre-3384aa41001147d28abf2ddbae80dea0-e0acd4e9c4064edabe7b00373eaa8bb0FakePre-6812d42ce7054a19912aa0fd637c522c-7e4a5807d62a42ef9cb697dd709eae10FakePre-2953d5ee97b849c59fa73f9882405a30-7bfbdfc06d0f4be9a9cba929462e8862FakePre-9ba283c396134e2986075cb3bde8100d-3553a29b0cf94cffa7ecf632e301e4a2 StoreController target = new StoreController(); int id = 669; ActionResult actual; actual = target.Details(id); Assert.IsNotNull(actual); Assert.IsInstanceOfType(actual, typeof(ViewResult)); ViewResult viewResult = (ViewResult)actual; Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(Album)); Album album = (Album)viewResult.ViewData.Model; Assert.AreEqual(id, album.AlbumId); Assert.AreEqual("Ring My Bell", album.Title);FakePre-1b9219d6c45449c3bed6a121beb36c7a-4422c93f4dc447d294b65227313df784FakePre-001f5abaf9bb4c9d9ba6a83ec777f757-dc140f006a574fb6b4164eb78eaebcfeFakePre-34f1ee6320db4adebb8f9ea318231df4-2839634f8d344a1181d1eb74f0c71299FakePre-36bedeed58bc4dcfba0fbd81461c598b-0d8dc27de1a64ba889fc92ed66b2a514FakePre-0520dbebd798490db315ecce44e1b43f-c4ab2ccf9af547cdaa0f4a144ddaa5b5

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 DetailsTestMethod – VB)

    Visual Basic

    ''' <summary>
    FakePre-f0c4a9cbfd5846debc8ffda74590c465-97e4c25fd7634a90bd0de28f6a456e1eFakePre-d58bd3c926634f998a68f18ffdfa2cbc-cd3ae8296c1f4d56b60ea3b7585fe1ecFakePre-fcdf51354a714d8e8903183f7273f9b5-0dd47d4df1e2425590e3b042c5c92cc9FakePre-4bc114997b7545fe89c4fa50ddcf17dc-e298967284a64e0ab9bf642acaa10f9c Dim target As New StoreController() Dim id As Integer = 669 Dim actual As ActionResult actual = target.Details(id) Assert.IsNotNull(actual) Assert.IsInstanceOfType(actual, GetType(ViewResult)) Dim viewResult As ViewResult = CType(actual, ViewResult) Assert.IsInstanceOfType(viewResult.ViewData.Model, GetType(Album)) Dim album As Album = CType(viewResult.ViewData.Model, Album) Assert.AreEqual(id, album.AlbumId) Assert.AreEqual("Ring My Bell", album.Title)FakePre-9488521fb4914e9bac36679ba226e7c5-a979136e3e08441e9237e6dee995c6e1FakePre-b56fc5bcd05b4e5580ae837888068068-9c46c95189e24d40a8ba3a882bc1bef7FakePre-af362e4ba855410b8489be2937d2d6f4-ae2e058f0ef5441985ab98ef9459b317FakePre-5b815349acc34ba6aad047cbe07e140e-020a64f833a54dcfb5e3bd942fd4136fFakePre-c6f363a57bc84c03b96aea2b4cac62f1-6df3a86aab20479b8a73a785fdc6767e

  3. Run the test. On the StoreControllerTest.[cs|vb] class right-click the DetailsTest method and select Run Tests

    Figure 9

    Running the DetailsTest test

Task 7 – Testing Browse Action Method for StoreController

In this task, you will learn how to test the Browse method. This method returns a list of albums for a specific genre.

  1. Open StoreControllerTest.[cs|vb] class and locate the BrowseTest method.
  2. Test getting the Albums list for a provided genre. To do this, replace the BrowseTest method with the following code.

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 BrowseTestMethod – CSharp)

    C#

    /// <summary>
    FakePre-63b292ca13b84ca280d5ccb41b292933-10d26a56e41e4eb7aa01907ccb25e2d9FakePre-64cf3080f83c4914b79b374f33f12310-cb9986879aeb43f1acb8c1447f5c7760FakePre-563aae94742049a2b9979fa46aa4daf6-3a7b35ee43df4dd2aff6d6e7db6c9a7fFakePre-e5b746b3d04546b2beb6463ebb8077bc-0118cb58052046da88e269d83f091450FakePre-d3a104eec98146c8ba30d5becdf45cfe-42cd554d983d4c52b576e7b14995247aFakePre-d87872d3fe8e458f8334d7c5d566c1d5-0f638e31e28c448d9e7d40b826bd0623FakePre-6b0bf82bc2a1435092723db9775df679-68a58eac9e0e4af8a4a97a4bf9c9fb59 StoreController target = new StoreController(); string genre = "Disco"; ActionResult actual; actual = target.Browse(genre); Assert.IsNotNull(actual); Assert.IsInstanceOfType(actual, typeof(ViewResult)); ViewResult viewResult = (ViewResult)actual; Assert.IsNotNull(viewResult.ViewData.Model); Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(StoreBrowseViewModel)); StoreBrowseViewModel model = (StoreBrowseViewModel)viewResult.ViewData.Model; Assert.AreEqual("Disco", model.Genre.Name); Assert.AreEqual(3, model.Albums.Count);FakePre-cceae0ba2fff4b33a2b1a8687c9138bc-766aaff8b30b4296b45722545e9a8abbFakePre-93eb69cffe8f4749943c68fd85feefbe-cf5c3ec4368a4fe39773483bd543317dFakePre-63f19bcca5ed4c8f9c7e5e506e51381f-00d2fcd1a63e4008b496ec9363be1b59FakePre-22a7fafa3b2743028f64293e7e75dd51-7c8720b931fe4c0db1ee096ef0929482FakePre-2ea3d8b9867a4557abfc17ffc39b65d6-19f40fb35b624cd09043927c15f92a4fFakePre-bb0bf9a345b44f72aa4cd5e4ea08c37d-55104da59c4f4555bd2c1cc6dea60a04

    (Code Snippet – ASP.NET MVC 3.0 Testing – Ex2 BrowseTestMethod – VB)

    Visual Basic

    ''' <summary>
    FakePre-d6de7a382d8e4b619241b2f5a04bb831-c696609e7c9341368eba7da679dbcc84FakePre-a4dfff0ebc2e4f4891326e97973b4204-14a42a768c47431da0a9392ebd978b93FakePre-f3ab677013114afba197f1aeb9952fd5-bb1e36856de94f408dcf96d898c7a2ecFakePre-57f56ba653084f9a89a34910b5dda7d1-73032d341c08495d8417420e2a5a7eef Dim target As New StoreController() Dim genre As String = "Disco" Dim actual As ActionResult actual = target.Browse(genre) Assert.IsNotNull(actual) Assert.IsInstanceOfType(actual, GetType(ViewResult)) Dim viewResult As ViewResult = CType(actual, ViewResult) Assert.IsNotNull(viewResult.ViewData.Model) Assert.IsInstanceOfType(viewResult.ViewData.Model, GetType(StoreBrowseViewModel)) Dim model As StoreBrowseViewModel = CType(viewResult.ViewData.Model, StoreBrowseViewModel) Assert.AreEqual("Disco", model.Genre.Name) Assert.AreEqual(3, model.Albums.Count)FakePre-018dacf2559f4e7a8fd4779b59bede8b-52287939cc084c4b9501beb5b8a81034FakePre-4d91c44888454ff48b558150c90c3413-f7279f04a6fe4b038ed149067d9619bbFakePre-2e79a58cdfa94bad9f3b8d2d2e9f7887-b40929c9be334ce7baf5b3b76152e9e4FakePre-1ab9dffb74f24ef08bcfe7971b177476-1db5c06e641742d79dcb6e6999675184FakePre-39df865f385040589f7fec121eb4d3b8-2f7f417db6cb415d9ed5ddd884b2cd21FakePre-fb909e27ee254c69b0773bfd479b73e0-60610ce8d2ae4b3aa36d7be741118a84

  3. This is the last test of the exercise where you will verify that all tests pass. To do this, in the Test menu, select Run and then All Tests in Solution.

    Figure 10

    Running all testsx in the solution

Next Step

Exercise 3: Testing cart actions