0

/在 C# 集合类库项目中使用 NUnit 测试我无法弄清楚这段代码中的 TestFixture 任何帮助都会有很大的帮助我有一个下面的测试类/

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ScoringExercise;
using ScoringExercise.Entities;

namespace ScoringExerciseTests
{
    [TestFixture]
    public class ScoringTests
    {
        readonly List<MultiChoiceItem> _assessmentItems;

        public ScoringTests()
        {
            // Assessment items
            _assessmentItems = new List<MultiChoiceItem>
            {
                new MultiChoiceItem()
                {
                    ItemText = "Which city is the capital of Sweden?",
                    Options = new string[] {"Helsinki", "Stockholm", "Malmö", "Oslo"},
                    CorrectAnswerIndex = 1,
                    MarksAwardedIfCorrect = 1
                },
                new MultiChoiceItem()
                {
                    ItemText = "Which of these cheeses normally has large round holes?",
                    Options = new string[] {"Emmental", "Feta", "Danish Blue", "Gruyere"},
                    CorrectAnswerIndex = 0,
                    MarksAwardedIfCorrect = 1
                },
                new MultiChoiceItem()
                {
                    ItemText = "Which of the following is not a root vegetable?",
                    Options = new string[] {"Carrot", "Parsnip", "Turnip", "Shallot"},
                    CorrectAnswerIndex = 3,
                    MarksAwardedIfCorrect = 4
                },
                new MultiChoiceItem()
                {
                    ItemText = "What colour is the outmost archery target ring?",
                    Options = new string[] {"White", "Yellow", "Red", "Black"},
                    CorrectAnswerIndex = 0,
                    MarksAwardedIfCorrect = 1
                },
                new MultiChoiceItem()
                {
                    ItemText = "What is the chemical symbol for silver?",
                    Options = new string[] {"Au", "Sr", "Si", "Ag"},
                    CorrectAnswerIndex = 3,
                    MarksAwardedIfCorrect = 2
                }
            };

        }

        [Test]
        public void AllCorrect()
        {
            // create test data where all items have corresponding responses
            // and all responses are correct
            Dictionary<int, int> responses = new Dictionary<int, int>();

            int i = 0;
            foreach (MultiChoiceItem item in _assessmentItems)
            {
                responses.Add(i, item.CorrectAnswerIndex);
                i++;
            }

            // check that actual results are in line with expected results
            AssessmentResults expected = new AssessmentResults()
            {
                ItemsAttempted = _assessmentItems.Count,
                ItemsCorrect = _assessmentItems.Count,
                TotalMarksAwarded = _assessmentItems.Sum(item => item.MarksAwardedIfCorrect)
            };
            AssessmentResults actual = ScoringEngine.GetResults(_assessmentItems, responses);

            AssertValueEquality(expected, actual);
        }

        [Test]
        public void AllWrong()
        {
            // create test data where all items have corresponding responses
            // and all responses are wrong
            Dictionary<int, int> responses = new Dictionary<int, int>();

            int i = 0;
            foreach (MultiChoiceItem item in _assessmentItems)
            {
                if ((item.CorrectAnswerIndex + 1) < item.Options.Length)
                    responses.Add(i, item.CorrectAnswerIndex + 1);
                else
                    responses.Add(i, item.CorrectAnswerIndex - 1);
                i++;
            }

            // check that actual results are in line with expected results
            AssessmentResults expected = new AssessmentResults()
            {
                ItemsAttempted = responses.Count,
                ItemsCorrect = 0,
                TotalMarksAwarded = 0
            };
            AssessmentResults actual = ScoringEngine.GetResults(_assessmentItems, responses);

            AssertValueEquality(expected, actual);
        }

        private void AssertValueEquality(AssessmentResults expected, AssessmentResults actual)
        {
            CollectionAssert.AreEqual(
                new int[] { expected.ItemsAttempted, expected.ItemsCorrect, 
                expected.TotalMarksAwarded },
                new int[] { actual.ItemsAttempted, actual.ItemsCorrect, 
                actual.TotalMarksAwarded } 
            );
        }
    }
}

/如何在我的类库项目中实现 GetResults 方法,如下所示我无法弄清楚这段代码中的 TestFixture 任何帮助都会有很大的帮助/

using System.Collections.Generic;
using ScoringExercise.Entities;
using System.Linq;
using System.Collections;

namespace ScoringExercise
{
    public static class ScoringEngine
    {
        /// <summary>
        /// Calculates the results of an assessment based upon the test content and candidate 
        /// responses.
        /// </summary>
        public static AssessmentResults GetResults(List<MultiChoiceItem> multiChoiceItems, 
        Dictionary<int, int> responses)
        {
            //return null;
        }
    }
}

namespace ScoringExercise.Entities
{
    /// <summary>
    /// Represents a single multi-choice item in an assessment
    /// </summary>
    public class MultiChoiceItem
    {
        // the text associated with the item aka the question
        public string ItemText { get; set; }
        // the option strings from which the candidate chooses a response
        public string[] Options { get; set; }
        // the index of the correct answer from within the Options array
        public int CorrectAnswerIndex { get; set; }
        // the number of marks awarded if the correct response is chosen
        public int MarksAwardedIfCorrect { get; set; }
    }
}

namespace ScoringExercise.Entities
{
    /// <summary>
    /// Represents the results of a single assessment instance
    /// </summary>
    public class AssessmentResults
    {
        public int ItemsAttempted { get; set; }
        public int ItemsCorrect { get; set; }
        public int TotalMarksAwarded { get; set; }
    }
}
4

1 回答 1

0
public static AssessmentResults GetResults(List<MultiChoiceItem> multiChoiceItems, Dictionary<int, int> responses)
        {
            int i = 0;
            int ItemsCorrect = 0;
            int TotalMarksAwarded = 0;

            foreach (var item in multiChoiceItems.Where(c => responses.ContainsKey(c.CorrectAnswerIndex)))
            {
                if (item.CorrectAnswerIndex == responses[i])
                {
                    ItemsCorrect++;
                    TotalMarksAwarded += item.MarksAwardedIfCorrect;
                }
                i++;
            }

            return new AssessmentResults
            {
                ItemsAttempted = responses.Count,
                ItemsCorrect = ItemsCorrect,
                TotalMarksAwarded = TotalMarksAwarded
            };
        }
    }
于 2020-12-02T18:48:45.240 回答