Giter VIP home page Giter VIP logo

Comments (4)

marcosnils avatar marcosnils commented on August 23, 2024

Matt,

The reason why this is happening is because Goblin executes everything after all the tests are declared. This means that in your first example, the value of the variable "i" in every test declared inside the loop will be the same.

In order to get your example to work you must do something like this:

for i, exp := range expectations {
    thing := things[i]
    g.It("each thing must be correct", func() {
    t.Log(len(thing.List)) // always prints 0
    t.Log(len(exp.List)) // always print 0
  })
}

Let me know if that works for you.

from goblin.

mattetti avatar mattetti commented on August 23, 2024

Sorry for the lack of feedback,

Quick question, can I do something like this (obviously the following code would fail:

for i, exp := range expectations {
   g.It("each thing must be correct", func(thing) {
    t.Log(len(thing.List))
    t.Log(len(exp.List))
  }(things[i]))
}

Basically, I'd like to send arguments to my expectation. This isn't required and I understand that the problem I encountered was a scoping bug in my code and I should have found it myself, but I'm curious to what extent, nested functions can cause more scoping issues.

from goblin.

marcosnils avatar marcosnils commented on August 23, 2024

@mattetti

Sorry for taking too long to respond. I get what you're trying to do, and I don't think we need to change goblin in order to accomplish that.

Let me know if this works for you:

g.Describe("test", func() {
  for i := 0; i < 5; i++ {
    (func(i int) {
        g.It("Is pending "+strconv.Itoa(i), func() {
            fmt.Println(i)
        })
     }(i))
  }
})

Marcos.

from goblin.

mattetti avatar mattetti commented on August 23, 2024

Oh I see, it delays the running of the "It" function since it's nested the
anonymous function. Smart!

On Mon, Dec 2, 2013 at 5:48 PM, Marcos Nils [email protected]:

@mattetti https://github.com/mattetti

Sorry for taking too long to respond. I get what you're trying to do, and
I don't think we need to change goblin in order to accomplish that.

Let me know if this works for you:

g.Describe("test", func() {
for i := 0; i < 5; i++ {
(func(i int) {
g.It("Is pending "+strconv.Itoa(i), func() {
fmt.Println(i)
})
}(i))
}})

Marcos.


Reply to this email directly or view it on GitHubhttps://github.com//issues/33#issuecomment-29676801
.

from goblin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.