Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6358865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:26:37+00:00 2026-05-24T23:26:37+00:00

I have a Repositry class wich initiates like this: public ContactRepository(string sqlStr, string username)

  • 0

I have a Repositry class wich initiates like this:

public ContactRepository(string sqlStr, string username)
{
    if (!string.IsNullOrEmpty(sqlStr))
    {
        ent = new AceoEntities(sqlStr);
        Username = username; 
    }
    else
        new Exception("No sql string is defined");
}

This might not be the best method, but I would like to make sure it’s not possible to create an instance off the class without sqlStr.

Then I’m trying to test this:

[TestMethod()]
public void CreateContactRepositoryWithEmtySqlString()
{
    string sqlStr = string.Empty;
    ContactRepository target;

    try
    {
        target = new ContactRepository("kvelland-kk", sqlStr);
    }
    catch (Exception e)
    {
        Assert.AreEqual("No sql string is defined",e.Message);
    }
}

My question is: Is this the correct way to to this? I’ having problems getting this to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:26:37+00:00Added an answer on May 24, 2026 at 11:26 pm

    I prefer GarethOwen’s answer (the ExpectedException attribute) or this way:

    public void MyTest()
    {
        try
        {    
            target = new ContactRepository("kvelland-kk", sqlStr); 
    
            Assert.Fail("Should have failed with MyExceptionType");
        }
        catch(MyExceptionType){}
    }
    

    Checking exception messages is not a good idea. Cause you may get a different message based on the system localisation. Check for the exception type instead. And as Xhalent mentioned don’t throw the a Exception, throw a specific type of exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T>
I have a repository of files which are unrelated to each other but are
I have a repository which I have already cloned from Subversion. I've been doing
We have an SVN repository running on a Windows server, and I want to
I have a subversion repository with the standard layout, i.e. trunk/ and branches/ (and
I have a git repository with remote foo. foo is a web app, is
I have a git repository which tracks an svn repository. I cloned it using
I have a SVN repository for my project; it keeps code, docs, graphs, etc.
I have a git repository with multiple branches. How can I know which branches
I have a Git repository I store random things in. Mostly random scripts, text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.