Chapter 13 â€“ Adding Automated Tests

  1. We have the following xUnit test method but it isn't being picked up by the test runner. What is wrong?
public void Minus_When2Integers_ShouldReturnCorrectInteger()
{
var result = Calc.Add(2, 1);
Assert.Equal(1, result);
}

The Fact attribute is missing.

  1. We have a string variable called successMessage in an xUnit test and we need to check that it contains the word "success". What method in the Assert class could we use?

Assert.Contains

  1. We have created some Jest unit tests on a List component in a file called ListTests.tsx. However, when the Jest test runner runs, the tests aren't picked up. Why is this so?

The test filename needs to end with .test.tsx. So, if we rename the file List.test.tsx, then the test will get picked up.

  1. We are implementing a test in Jest and we have a variable called result, which we want to check isn't null. Which Jest matcher function can we use?

expect(result).not.toBeNull();

  1. Let's say we have a variable called person that is of the, Person type:
interface Person {
id: number;
firstName: string;
surname: string
}

We want to check that the person variable is { id: 1, firstName: "Tom", surname: "Smith" }. What Jest matcher function can we use?

We can use the toEqual function to compare objects:

expect(person).toEqual({ id: 1, firstName: "Tom", surname: "Smith" });
  1. We are writing an end to end test using Cypress for a page. The page has a heading: Sign In. What Cypress command can we use to check that this is rendered okay?

We can use the following:

cy.contains('Sign In');
  1. We are writing an end-to-end test using Cypress for a page that renders the text Loading... while data is being fetched. How can we assert that this text is rendered and then disappears when the data has been fetched?

We can use the following:

cy.contains('Loading...');
cy.contains('Loading...').should('not.exist');

The first command will check that the page renders Loading... on the initial render. The second command will wait until the Loading... disappears—that is, the data has been fetched.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.95.216