使用rspec,您应该非常清楚如何组织单位规格。 spec
内的目录结构与app
目录中的目录结构非常相似,因此模型规范位于model
目录中,控制器规格位于controller
目录中,因此上。
但是集成测试并不是那么清楚。我只有一个与集成测试相关的文件:spec/features/integration.rb
是否需要创建一个精心设计的规范来测试应用程序的每个功能?像这样:
require 'spec_helper'
describe "Everything", js: true do
before do
@user_0 = FactoryGirl.build(:user_0)
@user_1 = FactoryGirl.build(:user_1)
@user_2 = FactoryGirl.build(:user_2)
@user_3 = FactoryGirl.build(:user_3)
end
it "can create a user" do
visit root_path
click_link 'Sign In'
ap @user_0
fill_in('Email', with: @user_0.email)
fill_in('Password', with: @user_0.password)
click_button 'Sign in'
visit('/user_friendships')
end
it "can create a user" do
end
it "can create a user" do
end
it "can create a user" do
end
it "GET /root_path" do
visit root_path
page.should have_content("All of our statuses")
click_link "Post a New Status"
page.should have_content("New status")
fill_in "status_content", with: "Oh my god I am going insaaaaaaaaane!!!"
click_button "Create Status"
page.should have_content("Status was successfully created.")
click_link "Statuses"
page.should have_content("All of our statuses")
page.should have_content("Jimmy balooney")
page.should have_content("Oh my god I am going insaaaaaaaaane!!! ")
end
end
但更长一点?
我应该使用多个文件吗?我该如何使用describe块?我现在只使用一个,感觉不对。
答案 0 :(得分:0)
简短的回答是:不,这并不意味着要进入一个档案。
较大的项目将其验收测试套件拆分为基于功能集的文件。如果您有很多测试,它们通常会分成不同的目录。组织这些测试的方式取决于您。我在这里看到了很多不同的方法。我倾向于对数据库设置的类似要求进行分组,即测试数据。
如果您想获得有关rspec测试的精彩指南,请访问以下网站:http://betterspecs.org/
答案 1 :(得分:0)
通常最好在应用程序中按页面或流划分集成测试。
例如,您可以尝试:
cd MyNewFiles
declare -a even_files
for x in *
do
[[ ${x} =~ [02468]$ ]] && even_files+=( "${x}" )
done
# Sample usage
rm "${even_files[@]}"
# spec/users/sign_in_spec.rb
RSpec.describe 'Users Sign In', js: true do
let!(:user) { FactoryGirl.build(:user_0) }
it "can sign in" do
visit root_path
click_link 'Sign In'
fill_in('Email', with: user.email)
fill_in('Password', with: user.password)
click_button 'Sign in'
page.should have_content('Signed In')
end
end
如果您正在寻找一种组织规范的方法,则可以使用Capybara Test Helpers来封装代码并使测试更具可读性。例如:
# spec/statuses/manage_status.rb
RSpec.describe 'Statuses', js: true do
it "should be able to post a status" do
visit root_path
page.should have_content("All of our statuses")
click_link "Post a New Status"
fill_in "status_content", with: "Everything is Alright"
click_button "Create Status"
page.should have_content("Status was successfully created.")
click_link "Statuses"
page.should have_content("All of our statuses")
page.should have_content("Everything is Alright ")
end
end
# spec/users/sign_in_spec.rb
RSpec.describe 'Users Sign In', js: true, test_helpers: [:login] do
let!(:user) { FactoryGirl.build(:user_0) }
it "can sign in" do
visit root_path
login.enter_credentials(email: user.email, password: user.password)
current_page.should.have_content('Signed In')
end
end