Home > OS >  Why is ERB not executing in Rails view spec?
Why is ERB not executing in Rails view spec?

Time:12-09

I'm trying to get some ERB to execute in a view spec.

I have a view spec that tests some simple ERB logic. Here is the view file:

<!DOCTYPE html>
<html>
  <body>
    <%= puts 'hello world' %>
  </body>
</html>

In my spec, I have this expectation:

expect(response.body).to include('hello')

However, the spec is not executing the ERB. The body is returned with some content, but the inside of <body></body> contains nothing.

Any ideas how to get ERB code to execute? This is probably a configuration issue. This spec is part of a Rails engine (just sharing if it helps in anyway).

When running the server locally, the ERB is executed and "hello world" appears. Trying to figure out how to get RSpec to execute the ERB.

CodePudding user response:

<%= %> This evaluates the expression and append the return value in its place. In your case the expression inside is puts("hello world") which will return nil.

So after execution nil is being appended inside the body tag. For this to work change it to

<!DOCTYPE html>
<html>
  <body>
    <%= 'hello world' %>
  </body>
</html>
  • Related