Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure a view exists

I'm currently looking into unit testing for a new application I have to create. I've got the basic testing going nicely (testing the ActionResult classes is pretty nice). One thing I do want to make sure though, is that a viewpage exists in my solution. I'm not 100% sure my test is correct, so if anyone had suggestions, please don't hesitate!

This is a test I have to check that my login method on my security controller is doing the right thing:

[TestMethod]
public void Login()
{
    var authProvider = new Mock<IAuthenticationProvider>();
    var controller = new SecurityController(authProvider.Object);

    var result = controller.Login() as ViewResult;

    Assert.IsNotNull(result, "ActionResult should be of type ViewResult.");
    Assert.AreEqual(result.ViewName, "login", "Does not render login page.");
}

My explanation of the test would be:

  • call the method 'Login' on the controller
  • Confirm it's rendering a view (by checking if it returns a ViewResult object)
  • Confirm it's rendering the right view (by checking the viewname)

What I would like to have is a third assert, to see if the view to be rendered actually exists.

Some secondary questions I have would be:

  • Should I split this test up?
  • Should I rename it (like, err, LoginRendersCorrectView or something)

Thanks!


Note: I'm explicitly trying to avoid having to check the filesystem. I'm sort of hoping for a way to use the ViewEngine to confirm the view actually exists.

like image 344
Erik van Brakel Avatar asked Jan 30 '09 00:01

Erik van Brakel


People also ask

How do you check if a view already exists in SQL?

Approach 4: Using OBJECT_ID() function But specifying Database Name and Schema Name provides an option to check the existence of the View in the specified database and within a specified schema, instead of checking in the current database across all the schemas.

Does View exist in database?

A view is a virtual table whose contents are defined by a query. Like a table, a view consists of a set of named columns and rows of data. Unless indexed, a view does not exist as a stored set of data values in a database.

What is a view and when should it be used?

Views are acceptable when you want to restrict users to a particular subset of data. For instance, if you do not delete records but only mark the current one as active and the older versions as inactive, you want a view to use to select only the active records.


1 Answers

  • No, I don't think you should split the test up as long as its just mainly a third assert and not very much more code.

  • Yes, I think a more descriptive name would be helpful.

  • Since you've verified it has the correct view name already, wouldn't simply successfully rendering the view verify its existence?

I think that its great you are working on complete test coverage but here I feel like there might be more effective use of your time if you were able to move on to the part where you verify that the units that perform the actual specific login functions (such as verifying password hashes or whatever) are working correctly.

like image 199
Jason Livesay Avatar answered Sep 22 '22 12:09

Jason Livesay