-
Notifications
You must be signed in to change notification settings - Fork 14
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
RSpec let! equivalent proposal #113
Comments
This is interesting. I see only one issue. this library allows to use lazy variables in But for this specific helper, I can actually say that the behavior is different (anyway it's different), so this is fine. |
However, I plan to rewrite library API to something like this. This will allow to greatly simplify integration with testing frameworks. But not sure when this happens. Don't have enough capacity |
I am +1 for this, but could live without it, too. RSpec also has A Scattered references I found regarding If |
That could be an approach! If would expect it to be before(:each), as different describe/context sections are able to override the variable as well. |
Yeah, my RSpec is getting a little rusty, but |
I would like the equivalent of Syntax suggestion: a regular My use case is beforeEach(async () => {
await SomeSequelizeService.create($params); // This line returns an ORM object, I would like to save it's id.
}); Not all methods in my In ruby I would just let!(:existing_object) { create :object}
let(:existing_object_id) { (existing_object.id } |
I often have to work with async functions. And this forces me to write a lot of await statements. describe("Board", function () {
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
const ONE_GWEI = 1_000_000_000;
def('contractFactory', async () => await ethers.getContractFactory("Board"));
def('owner', async () => await ethers.getSigner());
def('startTime', async () => await time.latest());
def('unlockTime', async () => (await $startTime) + ONE_YEAR_IN_SECS);
def('lockedAmount', () => ONE_GWEI);
subject(async () => {
return await (await $contractFactory).deploy($unlockTime, { value: $lockedAmount });
});
describe("Deployment", function () {
const TWO_YEAR_IN_SECS = 365 * 24 * 60 * 60;
def('unlockTime', async () => (await $startTime) + TWO_YEAR_IN_SECS);
it(async () => {
expect(await (await $subject).unlockTime()).to.equal(await $unlockTime)
});
});
}); It was possible to do without some async/await. I added them for clarity It would be great if the variables were waiting for the promise to complete. But with lazy variables this is obviously not possible(#5). But it is possible with let! variables. I suggest that when declaring a variable, add the optional ability to specify which variables must exist before this variable function is executed. And they are passed to function as parameters. def!('thirdVarName', (firstVar, secondVar) => firstVar + secondVar, ['firstVarName', 'secondVarName']); You can also specify lazy variables in this list. They are simply initialized as non-lazy along with this variable Thus, even before starting the test, it will be possible to disassemble in the order of initialization of variables. And at the same time keep the var overriding in child describes Example describe("Board", function () {
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
const ONE_GWEI = 1_000_000_000;
def!('contractFactory', () => ethers.getContractFactory("Board"));
def!('owner', () => ethers.getSigner());
def!('startTime', () => time.latest());
def('unlockTime', (startTime) => startTime + ONE_YEAR_IN_SECS, ['startTime']);
def('lockedAmount', () => ONE_GWEI);
subject!((contractFactory, unlockTime, lockedAmount) => {
return contractFactory.deploy(unlockTime, { value: lockedAmount });
}, ['contractFactory', 'unlockTime', 'lockedAmount']);
describe("Deployment", function () {
const TWO_YEAR_IN_SECS = 365 * 24 * 60 * 60;
def('unlockTime', (startTime) => startTime + TWO_YEAR_IN_SECS, ['startTime']);
it(async () => {
expect(await $subject.unlockTime()).to.equal($unlockTime)
});
});
}); |
The RSpec testing framework has lazy variable evaluation using
let(:some_var) { ... }
similar as this libarydef("someVar", () => ...)
. But there's also an additional helperlet!(:other_var) { ... }
(note the exclamation mark) which will evaluate the variable, despite it being referenced in an example or not.This comes in handy when a spec requires to setup some data which is not directly referenced. I'm currently doing to following for variables that need instantiation:
My proposal would be to introduce
deff
(or something similar) to replicate the "always instantiatie" behavior.What do you think?
The text was updated successfully, but these errors were encountered: