0
我正在使用Ruby on Rails 3.2.2和rspec-rails-2.8.1。我想在整个示例组中使用一个实例变量(在before
钩子中初始化),即使它在示例之外。也就是说,我愿提出以下:如何在整个示例组中使用实例变量,即使它在示例之外?
describe "..." do
before(:each) do
@user = User.create(...)
end
# Here I would like to use the instance variable but I get the error:
# "undefined method `firstname' for nil:NilClass (NoMethodError)"
@user.firstname
it "..." do
# Here it works.
@user.firstname
...
end
end
这可能吗?如果是这样,怎么样?
注:我想这样做,因为我想输出的更多信息有关将要运行的测试,这种方式:
# file_name.html.erb
...
# General idea
expected_value = ...
it "... #{expected_value}" do
...
end
# Usage that i am trying to implement
expected_page_title =
I18n.translate(
'page_title_html'
:user => @user.firstname # Here is the instance variable that is called and that is causing me problems
)
it "displays the #{expected_page_title} page title" do
view.content_for(:page_title).should have_content(expected_page_title)
end
为什么你甚至试图做到这一点呢?你能否详细说明你在做什么? – davidcelis 2012-04-06 20:14:30
@davidcelis - 我更新了问题。 – Backo 2012-04-07 00:41:21
@Backo为什么你需要把期望值放在规格描述中? – 2012-04-07 01:04:21