我对工厂女孩来说是全新的,对RSpec一般来说也是很新的。我正试图为用户设置一个标志作为一个工厂。我认为这将是工厂的一个很好的介绍。我认为我设置了工厂,但现在我需要知道如何在实际测试中实现它。我将展示到目前为止我所写的测试,也许我可以得到一些指导。
这里是工厂:
FactoryGirl.define do
factory :user do
email "cam@example.com"
password "password"
end
end以下是特性测试:
require "rails_helper"
RSpec.feature "Send a message" do
scenario "Staff can send a message" do
visit "/"
group = Group.create!(name: "Group A")
user = User.create!(email: "staff@example.com", password: "password")
fill_in "Email", with: "staff@example.com"
fill_in "Password", with: "password"
click_button "Sign in"
person = Person.create!(groups: [group], phone_number: "+161655555555")
message = Message.create(body: "Test Message", group_ids: [group.id])
fill_in "Enter a Message:", with: "Test Message"
check "message_group_#{group.id}"
click_button "Send Message"
expect(page).to have_content("Messages on their way!")
expect(page).to_not have_content("Body can't be blank")
expect(page).to_not have_content("Group ids can't be blank")
end
end我基本上是想用这块土地造一座工厂。这样我就不用一遍又一遍地重复这段代码了,对吧?
group = Group.create!(name: "Group A")
user = User.create!(email: "staff@example.com", password: "password")
fill_in "Email", with: "staff@example.com"
fill_in "Password", with: "password"
click_button "Sign in"发布于 2016-04-22 18:19:10
工厂的目的是创建数据,而不是执行一系列步骤。你想要的是为你做这件事的函数。
类似于这样的东西:
def sign_in
visit "/"
group = Group.create!(name: "Group A")
user = FactoryGirl.create(:user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end在你的测试中
scenario "Staff can send a message" do
sign_in
# Rest of code
end或
before(:each) { sign_in }
scenario "Staff can send a message" do
# Rest of code
end您可以在此文件、spec_helper / rails_helper或spec/support/文件中定义此函数。
https://stackoverflow.com/questions/36800303
复制相似问题