我开始使用rspec、水豚等,我想在Rails中做一些测试驱动的开发。
我正在使用的规范对页眉和页脚的外观有非常精确的定义,我认为这是一个开始学习的好地方。
对于页脚,我希望有以下规则:
如果用户已登录,则标题只包含徽标图像。徽标图像应该是指向用户登录页面的链接,这由用户拥有的权限决定。如果用户未登录,则该图像不是链接,并且其他四个链接也应该出现在表页脚中。
在erb中编写代码实际上是相当简单的,但我正在尝试做正确的事情,并在这里进行一系列测试。我的问题是,我似乎无法测试一个图像是否显示在屏幕上。我读过rspec这本书,但我不知道它在哪里说过“显示在资产目录中找到的此图像”。
因此,我的设置是在视图/布局目录中设置_header.html.erb _footer.html.erb _shim.html.erb application.html.erb
我认为我可以直接测试页脚部分,使用如下代码:
require "spec_helper"
describe "rendering views/layouts/_footer.html.erb" do
#from https://www.relishapp.com/rspec/rspec-rails/v/2-8/docs/view-specs/view-spec
it "shows the logo" do
render :template => "layouts/_footer.html.erb"
rendered.should =~ "/images/mainlogo.png"
end
describe "rendering views/layouts/_footer.html.erb as admin" do
before do
FactoryGirl.create(:admin_user)
end
it "links to landing from the logo"
render :template => "layouts/_footer.html.erb"
rendered.should contain("link/to/admin/landing")
end
end
#repeat landing tests for various user types然后,在其他页面中,我可以使用下面的内容简单地测试页脚本身是否存在
it should contain("footer")我的问题是,我甚至不能开始检查图像是否已经显示,更不用说图像是否与资源目录中的正确匹配了。我应该在这里做什么?
上面用于测试图像是否存在的代码(只是第一个describe/it块,而不是带有'as admin‘或’‘的代码)给出了以下警告和错误:
DEPRECATION WARNING: Passing a template handler in the template name is deprecated.
TypeError:
type mismatch: String given第一个可能是因为我使用了我不理解的语法,但第二个似乎表明比较的是字符串而不是资产。比较图像的语法是什么?有吗?
发布于 2013-01-31 15:53:47
您的类型不匹配错误是因为您正在将一个字符串传递给=~,它是一个正则表达式匹配器。您可以将字符串更改为正则表达式,也可以改用include。我选择include只是因为它更具可读性:
rendered.should include("/images/mainlogo.png")要消除弃用警告,只需从模板名称中删除.erb:
render :template => "layouts/_footer.html"https://stackoverflow.com/questions/14616126
复制相似问题