Feature Request: JustBeforeEachInverse
The way I write my tests is like this:
var _ = Describe("foo", func() {
var (
input string
actual string
)
BeforeEach(func() {
input = ""
})
JustBeforeEach(func(){
actual = foo(input)
})
When("input is bar", func() {
BeforeEach(func() {
input = "bar"
})
It("should return bar", func() {
Expect(actual).To(Equal("bar")
})
})
})
There are times though, that I want to add a JustBeforeEach within a nested context, and ensure this happens before the top-level JustBeforeEach.
E.g. maybe I have some other setup like
When("working with a single value, no commas", func() {
BeforeEach(func() {
writeFileToDisk(input)
})
When("the input file is named 'root'", func(){
JustBeforeEachInverse(func(){
input = "root"
})
It("should do something", func() {
...
})
})
})
Before running the top level JustBeforeEach (which is the "act"), I want to change the input in some way.
I keep running into this exact issue, and the only way I've found to solve it, is by dynamically creating the BeforeEach functions.
type entry struct {
when string
before func()
expectDesc string
expect func()
}
for _, ent := range []entry{
{
when: "a",
before: func() {
somethinghere
},
expectDesc: "it should error"
expect: func() {
Expect(...).To(...)
}
},
{
when: "b",
before: func() {
somethingelsehere
},
expectDesc: "it should not error"
expect: func() {
Expect(...).To(...)
}
},
} {
When(ent.when, func() {
BeforeEach(ent.before)
It(ent.expectDesc, ent.expect)
})
}
hey there - sorry for the delay. JustBeforeEach already adds a lot of complexity to the Ginkgo mental model - and I'm hesitant to double down on that complexity. For your usecase there may be other ways to structure the tests but if you want to stick with what you've got you may want to look at DescribeTableSubtree
It let's you do this:
DescribeTableSubtree("foo",
func(input string, expectation string) {
When("working with a single value, no commas", func() {
BeforeEach(func() {
writeFileToDisk(input)
})
It("should do something", func() {
Expect(whatever()).To(Equal(expectation))
})
})
},
Entry("with a root input file", "root", "super-user"),
Entry("with a twig input file", "twig", "leaf"),
)
you could even pass functions as parameters but I tend to shy away from that. it's usually possible to parametrize things with literals.
note that the signature of func can be anything you want and Ginkgo will take care of transferring the arguments passed in to Entry to the function (and checking types, etc.).