# What can prevent an integration test from loading instance variables?

**URL:** <https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374>\
**Category:** rubyonrails-talk\
**Created:** [February 10, 2017, 9:42am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374 "2017-02-10T09:42:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 9:42am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/1 "2017-02-10T09:42:13Z")

</div>

On 10 February 2017 at 09:16, 'krfg' via Ruby on Rails: Talk

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 10:23am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/2 "2017-02-10T10:23:23Z")

</div>

Only the test, the application works as expected. I thought the problem might be that there are too many nested partials in the homepage, but apart from that I have no idea why the test fails

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 10:33am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/3 "2017-02-10T10:33:49Z")

</div>

On 10 February 2017 at 10:23, 'krfg' via Ruby on Rails: Talk

> Only the test, the application works as expected. I thought the problem might be that there are too many nested partials in the homepage, but apart from that I have no idea why the test fails

Please remember to quote the previous message when replying, so that it is more easy to follow the thread.

Exactly what do you mean when you say the test is failing? Is the test itself failing or are you getting errors shown in the console when you run the test? In either case copy/paste the error here so we can get a better idea of what is going wrong. If you are not already doing so then just run the one test.

Colin

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 10:34am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/4 "2017-02-10T10:34:05Z")

</div>

You can have a look at the partial \_social\_layer.html.erb at [this link](https://www.railstutorial.org/book/following_users#code-home_page_stats): it is the code included in the if statement. As I said, I changed my homepage as follows:

\<% if logged\_in? %\> \<% if current\_user.gamer? %\> \<%= render ‘static\_pages/gamers\_home’ %\> \<% else %\> \<%= render ‘static\_pages/non\_gamers\_home’ %\> \<% end %\> \<% else %\> \<%= render ‘static\_pages/non\_logged\_in\_home’ %\> \<% end %\>

I reported an extract of static\_pages/gamers\_home in my first post. In theory all instance variables inside static\_pages\_controller.rb should be loaded by the test but they are not.

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 10:38am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/5 "2017-02-10T10:38:29Z")

</div>

On 10 February 2017 at 10:34, 'krfg' via Ruby on Rails: Talk

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 10:50am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/6 "2017-02-10T10:50:41Z")

</div>

Below is what the console is reporting when I run the test. As you can notice, the test raises one error: method any? on nil class (the instance variable @atp\_tournaments of the code in my first post). If I change this instance variable with its explicit value, the next error is of next method on next variable and so on.

me@SATELLITE-L50-A-161:~/workspace/fantasytennis\_app (tabbed-navigation)\*$ rails test test/integration/microposts\_interface\_test.rb Running via Spring preloader in process 7112 Started with run options --seed 4407

ERROR[“test\_micropost\_interface”, MicropostsInterfaceTest, 6.374447322000378] test\_micropost\_interface#MicropostsInterfaceTest (6.37s) ActionView::Template::Error: ActionView::Template::Error: undefined method `any?' for nil:NilClass app/views/static_pages/_gamers_home.html.erb:44:in `\_app\_views\_static\_pages\_\_gamers\_home\_html\_erb\_\_\_4018195177444297552\_78940320’ app/views/static\_pages/home.html.erb:3:in `_app_views_static_pages_home_html_erb___222411343280921082_78889880' app/controllers/microposts_controller.rb:12:in `create’ test/integration/microposts\_interface\_test.rb:16:in `block (2 levels) in <class:MicropostsInterfaceTest>' test/integration/microposts_interface_test.rb:15:in `block in class:MicropostsInterfaceTest’

2/2: [==========================================================================] 100% Time: 00:00:06, Time: 00:00:06

Finished in 6.63174s 2 tests, 8 assertions, 0 failures, 1 errors, 0 skips

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 11:03am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/7 "2017-02-10T11:03:39Z")

</div>

In app/controllers/static\_pages\_ controller.rb the following variables (among others) are defined:

def home if logged\_in? && !current\_user.gamer? … elsif logged\_in? && current\_user.gamer? … @week\_num = Time.now.strftime(“%W”).to\_i @atp\_tournaments = AtpCalendar.where(“week @\> ?”, “{#{@week\_num}}”) @wta\_tournaments = WtaCalendar.where(“week @\> ?”, “{#{@week\_num}}”) end end

The AtpCalendar and WtaCalendar fixtures are empty. Therefore, any? should return an empty array

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 11:30am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/8 "2017-02-10T11:30:11Z")

</div>

On 10 February 2017 at 11:02, 'krfg' via Ruby on Rails: Talk

> In app/controllers/static\_pages\_controller.rb the following variables (among others) are defined:
> 
> &nbsp;&nbsp;def home &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if logged\_in? && !current\_user.gamer? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;elsif logged\_in? && current\_user.gamer? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@week\_num = Time.now.strftime("%W").to\_i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@atp\_tournaments = AtpCalendar.where("week @\> ?", "{#{@week\_num}}")

Did you understand my previous post when I suggested that you should quote the previous message when replying?

Put some debug code to check that it is getting to the line above. You can use logger.info and the result will be printed in the log. So something like logger.info("atp tournaments count = #{@atp\_tournaments.count}"

Colin

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 11:50am UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/9 "2017-02-10T11:50:19Z")

</div>

Ok, I will quote. I would need information about logger.info When I try to open test.log Atom warns me that if it opens large files it might become unresponsive. Shall I take for granted that you are interested it test.log?

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 12:03pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/10 "2017-02-10T12:03:00Z")

</div>

My application test.log file is 107 MB

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 12:04pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/11 "2017-02-10T12:04:46Z")

</div>

On 10 February 2017 at 11:50, 'krfg' via Ruby on Rails: Talk

> I would need information about logger.info When I try to open test.log Atom warns me that if it opens large files it might become unresponsive. Shall I take for granted that you are interested it test.log?

Whichever log is relevant at the time, so when running tests then yes it is test.log. You can use tail to watch the log while you run the test tail -f log/test.log or you can use tail after the event just to show the last lines tail -n 100 log/test.log

Also you can delete or empty the log file before running the test, then it will only contain the latest information.

In fact I think logger.info outputs to the console too so you don't need to look at the log. Not certain about that though.

Colin

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 12:16pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/12 "2017-02-10T12:16:30Z")

</div>

Ok, thanks for the suggestion. I will try to use tail if it works with such a heavy file, otherwise I would need instructions on how to empty the file. The most important thing I have to understand is: where should I place logger.info? In static\_pages\_controller.rb before the declaration of @atp\_tournaments variable?

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 12:24pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/13 "2017-02-10T12:24:52Z")

</div>

On 10 February 2017 at 12:16, 'krfg' via Ruby on Rails: Talk

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 12:28pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/14 "2017-02-10T12:28:34Z")

</div>

On 10 February 2017 at 12:16, 'krfg' via Ruby on Rails: Talk

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 12:44pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/15 "2017-02-10T12:44:47Z")

</div>

As you suggested I inserted logger.info immediately after the declaration of @atp\_tournaments in static\_pages\_controller.rb:

def home if logged\_in? && !current\_user.gamer? … elsif logged\_in? && current\_user.gamer? … @week\_num = Time.now.strftime(“%W”).to\_i @atp\_tournaments = AtpCalendar.where(“week @\> ?”, “{#{@week\_num}}”) logger.info(“atp tournaments count = #{@atp\_tournaments.count}”) @wta\_tournaments = WtaCalendar.where(“week @\> ?”, “{#{@week\_num}}”) end end

Using tail -n 100 log/test.log I got the following outut:

atp tournaments count = 0

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 1:31pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/16 "2017-02-10T13:31:41Z")

</div>

On 10 February 2017 at 12:44, 'krfg' via Ruby on Rails: Talk

> ... As you suggested I inserted logger.info immediately after the declaration of @atp\_tournaments in static\_pages\_controller.rb:
> 
> &nbsp;&nbsp;def home &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if logged\_in? && !current\_user.gamer? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif logged\_in? && current\_user.gamer? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@week\_num = Time.now.strftime("%W").to\_i &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@atp\_tournaments = AtpCalendar.where("week @\> ?", "{#{@week\_num}}") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.info("atp tournaments count = #{@atp\_tournaments.count}") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@wta\_tournaments = WtaCalendar.where("week @\> ?", "{#{@week\_num}}") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> Using tail -n 100 log/test.log I got the following outut:
> 
> atp tournaments count = 0

Good, so now we can be confident that it is getting to that line. One thing that confuses me is that in an earlier post you showed the error:

ERROR["test\_micropost\_interface", MicropostsInterfaceTest, 6.374447322000378] test\_micropost\_interface#MicropostsInterfaceTest (6.37s) ActionView::Template::Error: ActionView::Template::Error: undefined method `any?' for nil:NilClass &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app/views/static_pages/_gamers_home.html.erb:44:in `\_app\_views\_static\_pages\_\_gamers\_home\_html\_erb\_\_\_4018195177444297552\_78940320' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app/views/static\_pages/home.html.erb:3:in `_app_views_static_pages_home_html_erb___222411343280921082_78889880' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app/controllers/microposts_controller.rb:12:in `create' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test/integration/microposts\_interface\_test.rb:16:in `block (2 levels) in \<class:MicropostsInterfaceTest\>' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;test/integration/microposts_interface_test.rb:15:in `block in \<class:MicropostsInterfaceTest\>'

which suggests that you are in the create method of the microposts controller. What I don't understand is how it then getting to the static pages controller. Can you post the start of microposts\_controller.rb (up to at least line 12) and also the start of static\_pages/home.html.erb.

Colin

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 1:46pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/17 "2017-02-10T13:46:23Z")

</div>

The home page is controlled by static\_pages\_controller.rb However the part of the home page concerning microposts creation and deletion is controlled by the microposts controller. Below is an extract of microposts\_controller.rb

class MicropostsController \< ApplicationController before\_action :logged\_in\_user, only: [:create, :destroy] before\_action :correct\_user, only: :destroy

def create @micropost = current\_user.microposts.build(micropost\_params) if @micropost.save flash[:success] = “Micropost created!” redirect\_to root\_url else @feed\_items = render ‘static\_pages/home’ end end

Line 12 is exactly render ‘static\_pages/home’ app/view/static\_pages/home.html.erb is all below:

\<% if logged\_in? %\> \<% if current\_user.gamer? %\> \<%= render ‘static\_pages/gamers\_home’ %\> \<% else %\> \<%= render ‘static\_pages/non\_gamers\_home’ %\> \<% end %\> \<% else %\> \<%= render ‘static\_pages/non\_logged\_in\_home’ %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![krfg](https://avatars.discourse-cdn.com/v4/letter/k/b5a626/32.png) [@krfg](https://discuss.rubyonrails.org/u/krfg)\
**Post date:** [February 10, 2017, 1:49pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/18 "2017-02-10T13:49:35Z")

</div>

> 

> The home page is controlled by static\_pages\_controller.rb However the part of the home page concerning microposts creation and deletion is controlled by the microposts controller. Below is an extract of microposts\_controller.rb
> 
> class MicropostsController \< ApplicationController before\_action :logged\_in\_user, only: [:create, :destroy] before\_action :correct\_user, only: :destroy
> 
> def create @micropost = current\_user.microposts.build(micropost\_params) if @micropost.save flash[:success] = “Micropost created!” redirect\_to root\_url else @feed\_items = render ‘static\_pages/home’ end end
> 
> Line 12 is exactly render ‘static\_pages/home’ app/view/static\_pages/home.html.erb is all below:
> 
> \<% if logged\_in? %\> \<% if current\_user.gamer? %\> \<%= render ‘static\_pages/gamers\_home’ %\> \<% else %\> \<%= render ‘static\_pages/non\_gamers\_home’ %\> \<% end %\> \<% else %\> \<%= render ‘static\_pages/non\_logged\_in\_home’ %\> \<% end %\>

The lines of the test that are mentioned by the test itself (lines 15 and 16) are:

```
assert_no_difference 'Micropost.count' do
  post microposts_path, params: { micropost: { content: "" } }
end

```

---

<div class="post-metadata">

**Author:** ![Colin\_Law](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Colin\_Law](https://discuss.rubyonrails.org/u/Colin_Law)\
**Post date:** [February 10, 2017, 1:58pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/19 "2017-02-10T13:58:04Z")

</div>

On 10 February 2017 at 13:46, 'krfg' via Ruby on Rails: Talk

> ... The home page is controlled by static\_pages\_controller.rb However the part of the home page concerning microposts creation and deletion is controlled by the microposts controller. Below is an extract of microposts\_controller.rb
> 
> class MicropostsController \< ApplicationController &nbsp;&nbsp;before\_action :logged\_in\_user, only: [:create, :destroy] &nbsp;&nbsp;before\_action :correct\_user, only: :destroy
> 
> &nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@micropost = current\_user.microposts.build(micropost\_params) &nbsp;&nbsp;&nbsp;&nbsp;if @micropost.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:success] = "Micropost created!" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to root\_url &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@feed\_items = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render 'static\_pages/home'

The render line does not invoke the static\_pages controller, it just invokes the view files for static\_pages/home, so it will not call the static\_pages controller to setup your variables. I don't know how your debug got to that code, I suspect it was from a different part of the test. Perhaps you meant redirect\_to which will invoke the controller to perform the action. You may find this useful -

> **[The difference between Render and Redirect in Rails](https://tosbourn.com/difference-between-redirect-render-rails/)**
>
> Find out the difference between Render and Redirect in Rails

Colin

---

<div class="post-metadata">

**Author:** ![hassan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/hassan/32/714_2.png) [@hassan](https://discuss.rubyonrails.org/u/hassan)\
**Post date:** [February 10, 2017, 2:19pm UTC](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374/20 "2017-02-10T14:19:06Z")

</div>

On Fri, Feb 10, 2017 at 3:02 AM, 'krfg' via Ruby on Rails: Talk

[Next page](https://discuss.rubyonrails.org/t/what-can-prevent-an-integration-test-from-loading-instance-variables/72374.md?page=2)
