Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: RSpec: check_text_answer #40

Open
2 tasks
tomsmith8 opened this issue Nov 12, 2024 · 0 comments
Open
2 tasks

Test: RSpec: check_text_answer #40

tomsmith8 opened this issue Nov 12, 2024 · 0 comments

Comments

@tomsmith8
Copy link
Owner

gpt-4o suggested coverage for: RSpec: check_text_answer


Stakwork Run


Unit Test Code


To generate RSpec tests for the check_text_answer method, we need to consider various scenarios, including when the answers match, when they don't, and when there are edge cases like trailing zeroes or decimals. We'll also need to mock the Job.strip_trailing_decimals_and_zeroes method to ensure our tests are isolated and focused on the logic within check_text_answer.

Here's how the RSpec tests could be structured:

describe '#check_text_answer' do
  # Main test suite for #check_text_answer

  let(:job) { double('Job') } # Mocking a Job instance

  before do
    # Allowing the method to be called on the job instance
    allow(job).to receive(:test_answer)
    allow(job).to receive(:user_answer)
  end

  context 'when test_answer and user_answer are identical' do
    # Test case for identical answers

    it 'returns true' do
      # Setup for identical answers
      allow(job).to receive(:test_answer).and_return('123.00')
      allow(job).to receive(:user_answer).and_return('123.00')

      # Mocks the stripping method to return the same cleaned value
      allow(Job).to receive(:strip_trailing_decimals_and_zeroes).with('123.00').and_return('123')

      # Call the method and assert the outcome
      expect(job.check_text_answer).to be true
    end
  end

  context 'when test_answer and user_answer differ' do
    # Test case for different answers

    it 'returns false' do
      # Setup for different answers
      allow(job).to receive(:test_answer).and_return('123.00')
      allow(job).to receive(:user_answer).and_return('124.00')

      # Mocks the stripping method to return different cleaned values
      allow(Job).to receive(:strip_trailing_decimals_and_zeroes).with('123.00').and_return('123')
      allow(Job).to receive(:strip_trailing_decimals_and_zeroes).with('124.00').and_return('124')

      # Call the method and assert the outcome
      expect(job.check_text_answer).to be false
    end
  end

  context 'when test_answer and user_answer have trailing zeroes' do
    # Test case for answers with trailing zeroes

    it 'returns true if they are numerically equivalent' do
      # Setup for answers with trailing zeroes
      allow(job).to receive(:test_answer).and_return('123.00')
      allow(job).to receive(:user_answer).and_return('123.0')

      # Mocks the stripping method to return the same cleaned value
      allow(Job).to receive(:strip_trailing_decimals_and_zeroes).with('123.00').and_return('123')
      allow(Job).to receive(:strip_trailing_decimals_and_zeroes).with('123.0').and_return('123')

      # Call the method and assert the outcome
      expect(job.check_text_answer).to be true
    end
  end

  context 'when test_answer or user_answer is nil' do
    # Test case for nil answers

    it 'returns false' do
      # Setup for nil answers
      allow(job).to receive(:test_answer).and_return(nil)
      allow(job).to receive(:user_answer).and_return('123.00')

      # Mocks the stripping method to handle nil gracefully
      allow(Job).to receive(:strip_trailing_decimals_and_zeroes).with(nil).and_return(nil)
      allow(Job).to receive(:strip_trailing_decimals_and_zeroes).with('123.00').and_return('123')

      # Call the method and assert the outcome
      expect(job.check_text_answer).to be false
    end
  end
end

Explanation:

  • let(:job): We use a double to mock a Job instance, allowing us to control the test_answer and user_answer values.
  • before block: Sets up the basic expectations for the test_answer and user_answer methods on the job instance.
  • allow(Job).to receive(:strip_trailing_decimals_and_zeroes): Mocks the strip_trailing_decimals_and_zeroes method to return controlled outputs, ensuring the test focuses on the logic of check_text_answer.
  • context blocks: Each context represents a different scenario, such as identical answers, different answers, trailing zeroes, and nil values.
  • it blocks: Each it block contains a specific test case, with comments explaining the setup and expected behavior.

Acceptance Criteria

  • I have estimated time taken to correct GPT mistakes in a comment on this ticket
  • I have provided feedback on GPT mistakes in a comment on this ticket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant