我有功能测试,其中有 2 个重复的部分:
require 'spec_helper'
describe "Messages manage" do
let (:user) { create :user }
let (:other_user) { create :use }
before(:each) do
login_as user
end
describe "[message create]" do
it "messages created by self, should appear on the page", js: true do
message_text = "Hello my friend"
visit user_messages_path(user, {with:other_user.id} )
fill_in :message_body, with: message_text
click_button t("users.messages.index.send")
sleep 1 # waiting for js
page.should have_content(message_text)
find_field(:message_body).value.should == ""
page.should have_selector('#message_body', visible: false)
end
it "message errors should be displayed", js: true do
message_text = "123"
visit user_messages_path(user, {with:other_user.id} )
fill_in :message_body, with: message_text
click_button t("users.messages.index.send")
sleep 1 # waiting for js
page.should_not have_content("Message: #{message_text}")
find_field(:message_body).value.should == message_text
page.should have_selector('.message.error', visible: true)
end
end
end
在这种情况下,使用 shared_examples 或类似方法的最佳方法是什么?跟随 DRY。