How does "beforeEach" work / question on unit testing in lesson 7 (~11 hours 24 minutes in) #1674
-
If I am going to run a test, such as (some pseudo-code for readability): describe("FundMe", function() {
beforeEach(async function() {
await deployments.fixture(["all"]) // deploy all scripts with "all" tag
fundMe = await getContract("FundMe", deployer) // most recently deployed fundme contract, which we just deployed.
})
describe("fund", function() {
it("Updates the amount funded data structure", async function() {
await fundMe.fund({ value: sendValue })
const response = await fundMe.addressToAmountFunded(deployer)
assert.equal(response.toString(), sendValue.toString())
})
it("Adds funder to array of funders", async function() {
await fundMe.fund({ value: sendValue })
const funder = await fundMe.funders(1) // NOTE: I changed this from PC's code from 1 to 0 for this discussion.
assert.equal(funder, deployer)
})
}) Testing both "it" statements within the "fund" describe function will revert on test #2. I had assumed either 1 or 0 for the funders index would work, since - I thought - this would be the second fundMe.fund() call to the most recently deployed fundMe contract. Since it reverts, does this suggest the "beforeEach" is called before each "it" function (not just before each describe)? More succinctly: How many times is fundMe deployed in the above test? Does "beforeEach" run before each subsequent "describe" or before each subsequent "describe->it"? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
@tmcdonough No, you are confusing here, each function runs a new copy you can say until you do not have beforeEach. Here you have only 1 beforeEach that deploys and gets the instance of the FundMe contract, after that it behaves that there is no account 0 amount funded to contract. Your first test is correct but for the second you need to compare it with funders(0). |
Beta Was this translation helpful? Give feedback.
-
A brief snapshot for beforeEachdescribe('hooks', function () {
before(function () {
// runs once before the first test in this block
});
after(function () {
// runs once after the last test in this block
});
beforeEach(function () {
// runs before each test in this block
});
afterEach(function () {
// runs after each test in this block
});
// test cases
}); |
Beta Was this translation helpful? Give feedback.
@tmcdonough No, you are confusing here, each function runs a new copy you can say until you do not have beforeEach.
Here you have only 1 beforeEach that deploys and gets the instance of the FundMe contract, after that it behaves that there is no account 0 amount funded to contract.
Your first test is correct but for the second you need to compare it with funders(0).