lundi 25 janvier 2021

Using a file fixture in a fixture file in Ruby on Rails

I’ve been looking for information about how to specify resources in the fixtures/files directory in my fixtures YAML files. For example, if I have the following fixture in fixtures/products.yml:

one:
  title: MyString
  description: MyText
  image_url: MyString
  price: 9.99

I have this test:

class ProductsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @product = products(:one)
  end

  test "should get index" do
    get products_url
    assert_response :success
  end
...

This in products/index.html.erb:

...
    <% @products.each do |product| %>
      <tr>
        <td>
          <%= image_tag(product.image_url) %>
...

and this action in the products controller:

class ProductsController < ApplicationController
  def index
    @products = Product.all
  end
...

I want to reference the file fixtures/files/image.png so that the URL and image are available for the test. How can I do that? is there a better way to do it?

I'm using Rails 6.0.3.4 and ruby 2.7.2

Aucun commentaire:

Enregistrer un commentaire