Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you should be able to access the same database in your test application as the one you're using in your main/actual application. You just need to make sure that your Test project contains your connection string in its own App.config.</p> <p>The initialization of the context should be done either <strong>inside</strong> your StegApp(), or you should be able to pass a context to your StegApp() from a different scope. From what I read of your code, your StegApp() will not be able to access the dataContext variable you created.</p> <p>Your test for null user already happens inside the RetrieveKeyFn() under the AuthenticateCredentials() method so there's no need for the first "Assert.IsNotNull(user)". I would recommend separating your business logic for RetrieveKeyFn from your UI behaviors so that you can easily do unit tests. You can bind the "Messagebox" operations to say a button click event handler which calls just RetrieveKeyFn(). I would suggest maybe something like this:</p> <pre><code>public class StegApp { public DatabaseEntities context; //other properties public StegApp() { //assuming your DatabaseEntities class inherits from DbContext. //You should create other constructors that allow you to set options //like lazy loading and mappings this.context = new DatabaseEntities(); } //ASSUMING YOUR RetrieveLogs.RetrieveMessages() function returns //a Message object. replace this type with whatever type the //RetrieveLogs.RetrieveMessages() method returns. public Message RetrieveKeyFn (string username, string password) { BusinessObjects.User p = RetreiveLogs.AuthenitcateCredentials(username,password); if (p != null) { var message = RetrieveLogs.RetrieveMessages(p.UserId); if (message == null) // handle behavior for no messages. In this case // I will just create a new Message object with a -1 LogId return new Message {LogId =-1}; else return message; } else //handle behavior when the user is not authenticated. //In this case I throw an exception throw new Exception(); } //on your button click handler, do something like: // try // { // var message = RetrieveKeyFn(txtUsername.Text.Trim(), txtPassword.Text.Trim()); // if (message.LogId == -1) // DisplayLogs.Text = "Sorry No messages for you recorded in Database, your correspondant might have chose not to record the entry"; // else // { // MessageBox.Show("Log Id = " + message.LogId) // etc. etc. etc. // } // } // catch // { // MessageBox.Show ("user is not authenticated"); // } } </code></pre> <p>When you do your unit test, <strong>remember to have the appropriate configuration strings in your test project's App.Config</strong> If the app.config does not yet exist, go ahead and create one. You should create tests for all possibilities (i.e. 1) user is valid, you get the message, 2) user is valid, there are no messages, 3) user is invalid).</p> <p>Here's an example for case 2</p> <pre><code> [TestMethod] public void RetrieveKeyFnTest1() { StegApp target = new StegApp(); // this creates your context. I'm assuming it also creates your RetrieveLogs object, etc var username = "UserWithNotMessages"; //this user should exist in your database but should not have any messages. You could insert this user as part of your TestInitialize method var password = "UserWithNotMessagesPassword"; //this should be the proper password var message = target.RetrieveKeyFn(username, password); Assert.AreEqual (-1, message.LogId); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload