# Spec example description

**URL:** <https://forum.crystal-lang.org/t/spec-example-description/1296>\
**Category:** Help & Support\
**Created:** [October 27, 2019, 11:46am UTC](https://forum.crystal-lang.org/t/spec-example-description/1296 "2019-10-27T11:46:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [October 27, 2019, 11:46am UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/1 "2019-10-27T11:46:37Z")

</div>

I am looking at the source code of crystal/src/spec/example.cr and I would love to have access to the Example’s description field from my specs.

People may be asking why would you want to do that ? The reason is that I have a lot of golang specs in the form of

```auto
t = {text: "thing to be tested", error: "some error", result: false}

```

I am editing those tests to get them in the following form (it is a lot of work)

```auto
it "thing to be tested" do
 test_example = `insert the description as a variable like Spec::Example.get_description()`
 ... do test with test_example ...
end

```

Thanks for reading and/or helping

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [October 27, 2019, 12:44pm UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/2 "2019-10-27T12:44:59Z")

</div>

Sorry, I don’t follow what you need the description for. Can you show an _actual_ example of what you want to do?

---

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [October 27, 2019, 1:33pm UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/3 "2019-10-27T13:33:21Z")

</div>

Let say I want to test some math operation

```auto
it "1 + 2" do
  expr = "1 + 2"
  mytest(expr)
end

```

I would like to do this

```auto
it "1 + 2" do
  expr = get_description_of_this_spec()
  # expr should no hold "1 + 2"
  mytest(expr)
end

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [October 27, 2019, 3:49pm UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/4 "2019-10-27T15:49:07Z")

</div>

Maybe the spec runner could yield the instance of `Spec::Example` to the block. This might even have been proposed before, I’m not sure. But it currently doesn’t work.

A simple workaround would be to use a helper method to build the example:

```auto
def it_exp(description, file = __FILE__ , line = __LINE__ , &block)
  it description, file: file, line: line do
    block.call(description)
  end
end

it_exp "1 + 2" do |expr|
  mytest(expr)
end

```

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [October 27, 2019, 4:01pm UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/5 "2019-10-27T16:01:47Z")

</div>

But what do you want to do with the description? In your example you pass it to a method, but what does the method do? We need to know the exact use case. For example if you need the description to show it when the spec fails, that’s already being done by the spec runner. Then, there’s no eval in Crystal so you can’t really work with that expression.

So what do you need the Example instance for?

That said, I wouldn’t mind adding it as a block argument, but please provide a real use case.

---

<div class="post-metadata">

**Author:** ![gummybears](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/gummybears/32/1886_2.png) [@gummybears](https://forum.crystal-lang.org/u/gummybears)\
**Post date:** [October 27, 2019, 4:02pm UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/6 "2019-10-27T16:02:49Z")

</div>

Thanks, great, exactly what I need.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [October 27, 2019, 6:49pm UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/7 "2019-10-27T18:49:16Z")

</div>

I guess a use case would be some kind of parser or anything that somehow handles a string. Then it would make sense to use the same string for both the description of the spec as well as the sample being tested.

This use case is present in the compiler specs. `it_parses` from `parser_spec.cr` is pretty similar to my workaround. But as with `it_parses`, I suppose that most use cases would warrant a custom helper method anyway which also provides additional setup and even the matcher itself (`it_parses` doesn’t receive a block).

---

<div class="post-metadata">

**Author:** ![carcinocron](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/carcinocron/32/1022_2.png) [@carcinocron](https://forum.crystal-lang.org/u/carcinocron)\
**Post date:** [April 5, 2020, 5:20pm UTC](https://forum.crystal-lang.org/t/spec-example-description/1296/8 "2020-04-05T17:20:47Z")

</div>

I need this. I need to use Spec::Item.description for the name of each test’s snapshot file. Otherwise each test will have 2 copies of the description text.

bad:

```auto
  it "lists some JSON for user" do
    res = process_request req
    res.body.should match_snapshot "Http/Posts/List/lists some JSON for user"
  end

```

good:

```auto
  it "lists some JSON for user" do
    res = process_request req
    res.body.should match_snapshot "Http/Posts/List/#{self.description}"
  end

```
