Rails测试:在视图上测试image_tag

时间:2021-09-10 00:26:58

I'm writing the view tests and I want to test an image:

我正在编写视图测试,我想测试一个图像:

<%= image_tag "icon.png", class: "img-responsive" %>

So I made this test, but is not working:

所以我做了这个测试,但是没有工作:

it 'app logo' do
  render
  assert_select 'img.img-responsive[src=?]', asset_path('icon.png')
end

It works if I use the following, but I think that it is not the right way of doing it:

如果我使用以下内容,它可以工作,但我认为这不是正确的方法:

it 'app logo' do
  render
  assert_select 'img.img-responsive[src=?]', '/assets/icon.png'
end

What could be the problem? Is this the correct why to test it with rspec?

可能是什么问题呢?这是正确的为什么用rspec测试它?

2 个解决方案

#1


0  

Try this using capybara:

使用水豚尝试这个:

page.should have_selector(:xpath, XPATH_OF_THE_IMAGE_ELEMENT)

#2


0  

I am not sure if it the method in the question is the correct or incorrect way but you can try the following:

我不确定问题中的方法是正确还是错误,但您可以尝试以下方法:

expect(response.body).to include('icon.png')
expect(response.body).to have_css(".img-responsive")

#1


0  

Try this using capybara:

使用水豚尝试这个:

page.should have_selector(:xpath, XPATH_OF_THE_IMAGE_ELEMENT)

#2


0  

I am not sure if it the method in the question is the correct or incorrect way but you can try the following:

我不确定问题中的方法是正确还是错误,但您可以尝试以下方法:

expect(response.body).to include('icon.png')
expect(response.body).to have_css(".img-responsive")