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

  • SEARCH
  • Home
  • 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 6861421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:31:57+00:00 2026-05-27T02:31:57+00:00

I have a strange behavior in a tests where I want to test that

  • 0

I have a strange behavior in a tests where I want to test that an exception is thrown when null is passed in as a parameter. When I run the test I get from NUnit:

    System.ArgumentNullException was expected
    -- Exception doesn't have a stack trace -- 

My test:

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Should_not_retrieve_any_fields_when_file_is_null()
{
    _creator.CreateFields(null);
}

My implementation:

public IEnumerable<ImportField> CreateFields(HttpPostedFileBase file)
{
    if (file == null) throw new ArgumentNullException("file");

    using (var reader = new StreamReader(file.InputStream))
    {
        var firstLine = reader.ReadLine();
        var columns = firstLine.Split(new[] { ',' });

        for (var i = 0; i < columns.Length; i++)
        {
            yield return new ImportField(columns[i], i);
        }
    }
}

Is there a logical explanation to this behavior and should I make my implementation differently?

  • 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-27T02:31:58+00:00Added an answer on May 27, 2026 at 2:31 am

    The reason you’re getting this behaviour is because of the yield keyword. When using yield, the compiler will generate a class for the method with the yield in it. When calling that method, control is unconditionally returned to back to the caller. Nothing in your method is actually executed before it is need.

    If you extract your using statement into a separate method and return the result, your test will pass. Or you can store the result to a variable in your test, and for example call “ToList()” on it.

        public IEnumerable<ImportField> CreateFields(HttpPostedFileBase file)
        {
            if (file == null) throw new ArgumentNullException("file");
    
            return ExtractFromFile(file);
        }
    
        private IEnumerable<ImportField> ExtractFromFile(HttpPostedFileBase file)
        {
            using (var reader = new StreamReader(file.InputStream))
            {
                var firstLine = reader.ReadLine();
                var columns = firstLine.Split(new[] { ',' });
    
                for (var i = 0; i < columns.Length; i++)
                {
                    yield return new ImportField(columns[i], i);
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some strange behavior redirecting stdout and stderr from s3cmd. For example: $
I have a horizontal animated menu that has a strange behavior because it works
I have come across a strange behavior of Java that seems like a bug.
I am getting some strange behavior involving database queries that I have never seen
I'm getting some strange behavior from a listview/the getChildAt method. I have a HashSet,
I have a simple HTML (as HTA) application that shows strange behavior on Windows
I have two kinds of Unit tests (not integration test). Because of some strange
I have strange behaviour in Python/PyMongo. dbh = self.__connection__['test'] first = dbh['test_1'] second =
I have realy strange behavior. I have this code (I remove unrelated code). $(function()
I have this strange behavior in my JSP page: I have two Integer variables

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.