Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

testing - 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

question from:https://stackoverflow.com/questions/65884213/using-a-file-fixture-in-a-fixture-file-in-ruby-on-rails

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...