我正在测试以下内容:
- 用户创建一个帖子(新页面),然后他被重定向到显示页面(帖子保存为“已发布”)。
- 他进入编辑页面,然后单击“保存草稿”按钮,他再次被重定向到显示页面。
- 他再次进入编辑页面,现在他应该看到如下内容:(
<p>Status: <span>"Draft"</span></p>
以前是<p>Status: <span>"Published"</span></p>
)
规格:
describe "post status" do
let!(:post) { FactoryGirl.create(:post, user: user,
title: "Lorem",
content: "Lorem Ipsum",
status: "Published",
#category_id: category.id,
tag_list: "#{tag.id}") }
before do
sign_in user
visit edit_post_path(post)
end
describe "edit page" do
it { should have_selector('h1', text: "Update post") }
it { should have_selector('span', text: "Published") }
end
let(:save_draft) { "Save Draft" }
let(:publish) { "Publish" }
describe "save as draft" do
before do
click_button save_draft
visit edit_post_path(post)
end
it { should have_selector('h1', text: "Update post") }
it { should_not have_selector('span', text: "Published") }
it { should have_selector('span', text: "Draft") }
end
end
<p class="status">
<% if @post.status == "Draft" %>
Status: <span class="label"><%= @post.status %></span>
<% elsif @post.status == "Published" %>
Status: <span class="label label-primary"><%= @post.status %></span>
<% end %>
</p>
编辑页面:
<p class="status">
<% if @post.status == "Draft" %>
Status: <span class="label"><%= @post.status %></span>
<% elsif @post.status == "Published" %>
Status: <span class="label label-primary"><%= @post.status %></span>
<% end %>
</p>
该行为在实时页面中有效,但是当我运行规范时,测试失败:
失败:
1)发布页面显示页面发布状态另存为草稿失败/错误:它{应该有_选择器('span',文本:“草稿”)}期望css“跨度”和文本“草稿”返回一些东西# ./spec/requests /post_pages_spec.rb:211:in `block (5 levels) in '
2) 发布页面显示页面发布状态另存为草稿失败/错误:它 { should_not have_selector('span', text: "Published") } 预期 css "span" 与文本 "Published" 不返回任何东西 # ./spec/ requests/post_pages_spec.rb:210:in `block (5 levels) in '
该规范似乎在标签内找到了文本“Published”,但没有找到“ Draftspan
” (它应该找到“Draft”)。
是否在错误的时间触发了点击按钮?如何解决?
编辑:
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post]) && params[:commit] == "Publish"
@post.update_attributes(status: "Published")
flash[:success] = "Post updated."
redirect_to @post
elsif @post.update_attributes(params[:post]) && params[:commit] == "Save Draft"
@post.update_attributes(status: "Draft")
flash[:success] = "Post draft updated."
redirect_to @post
else
render 'edit'
end
end
def create
@post = current_user.posts.build(params[:post])
if @post.save && params[:commit] == "Publish"
@post.update_attributes(status: "Published")
flash[:success] = "Post published."
redirect_to @post
elsif @post.save && params[:commit] == "Save Draft"
@post.update_attributes(status: "Draft")
flash[:success] = "Post saved as draft."
redirect_to @post
else
render 'new'
end
end