How to use before method of FactoryBot Package

Best Factory_bot_ruby code snippet using FactoryBot.before

user_pages_spec.rb

Source:user_pages_spec.rb Github

copy

Full Screen

1require 'rails_helper'2RSpec.describe 'UserPages', type: :request do3 subject { page }4 describe 'signup page' do5 before { visit signup_path }6 it { should have_content('注册') }7 it { should have_title('注册') }8 end9 describe 'profile page' do10 let(:user) { FactoryBot.create(:user) }11 let!(:m1) { FactoryBot.create(:micropost, user: user, content: "Foo") }12 let!(:m2) { FactoryBot.create(:micropost, user: user, content: "Bar") }13 before { visit user_path(user) }14 it { should have_content(user.name) }15 it { should have_title(user.name) }16 describe "microposts" do17 it { should have_content(m1.content) }18 it { should have_content(m2.content) }19 it { should have_content(user.microposts.count) }20 end21 describe "follow/unfollow buttons" do22 let(:other_user) { FactoryBot.create(:user) }23 before { sign_in user }24 describe "following a user" do25 before { visit user_path(other_user) }26 it "should increment the followed user count" do27 expect do28 click_button "Follow"29 end.to change(user.followed_users, :count).by(1)30 end31 it "should increment the other user's followers count" do32 expect do33 click_button "Follow"34 end.to change(other_user.followers, :count).by(1)35 end36 describe "toggling the button" do37 before { click_button "Follow" }38 it { should have_xpath("//input[@value='Unfollow']") }39 end40 end #following a user41 describe "unfollowing a user" do42 before do43 user.follow!(other_user)44 visit user_path(other_user) 45 end46 it "should decrement the followed user count" do47 expect do48 click_button "Unfollow"49 end.to change(user.followed_users, :count).by(-1)50 end51 it "should decrement the other user's followers count" do52 expect do53 click_button "Unfollow"54 end.to change(other_user.followers, :count).by(-1)55 end56 describe "toggling the button" do57 before { click_button "Unfollow" }58 it { should have_xpath("//input[@value='Follow']") }59 end60 end # unfollowing a user61 end # follow/unfollow62 describe 'search assignments link' do63 before do64 sign_in user65 visit home_path66 end67 it{ should_not have_link('更新作业')}68 describe 'as an admin user' do69 let(:admin) { FactoryBot.create(:admin) }70 before do71 sign_in admin72 visit home_path73 end74 it { should have_link('更新作业')}75 end76 end77 end #profile page78 describe 'signup' do79 before { visit signup_path }80 let(:submit) { '注册' }81 describe 'with invalid information' do82 it 'should not create a user' do83 expect { click_button submit }.not_to change(User, :count)84 end85 end86 describe 'with valid information' do87 before do88 fill_in '用户名', with: 'Example User'89 fill_in '邮箱', with: 'user@example.com'90 fill_in '密码', with: 'foobar'91 fill_in '请确认密码', with: 'foobar'92 end93 it 'should create a user' do94 expect { click_button submit }.to change(User, :count).by(1)95 end96 describe "after saving the user" do97 before { click_button submit }98 let(:user) { User.find_by(email: 'user@example.com') }99 it { should have_link('退出')}100 it { should have_title(user.name) }101 it { should have_selector('div.alert.alert-success', text: 'Welcome') }102 end103 end104 end105 describe "edit" do106 let(:user) { FactoryBot.create(:user) }107 before do108 sign_in user109 visit edit_user_path(user)110 end111 # describe "forbidden attributes" do112 # let(:params) do113 # { user: { admin: true, password: user.password,114 # password_confirmation: user.password } }115 # end116 # #let(:test_path) {user_path(user)}117 # before { patch user_path(user), params } #这行行不行?118 # specify { expect(user.reload).not_to be_admin }119 # end120 describe "page" do121 it { should have_content("修改信息") }122 it { should have_title("Edit user") }123 it { should have_link('修改头像', href: 'http://gravatar.com/emails') }124 end125 describe "with invalid information" do126 before { click_button "保存" }127 it { should have_content('error') }128 end129 describe "with valid information" do130 let(:new_name) { "New Name" }131 let(:new_email) { "new@example.com" }132 before do133 fill_in '用户名', with: new_name134 fill_in "邮箱", with: new_email135 fill_in "密码", with: user.password136 fill_in "请确认密码", with: user.password137 click_button "保存"138 end139 it { should have_title(new_name) }140 it { should have_selector('div.alert.alert-success') }141 it { should have_link('退出', href: signout_path) }142 specify { expect(user.reload.name).to eq new_name }143 specify { expect(user.reload.email).to eq new_email }144 end145 end146 describe "index" do147 # before do148 # sign_in FactoryBot.create(:user)149 # FactoryBot.create(:user, name: "Bob", email: "bob@example.com")150 # FactoryBot.create(:user, name: "Ben", email: "ben@example.com")151 # visit users_path152 # end153 let(:user) {FactoryBot.create(:user)}154 before(:each) do155 sign_in user156 visit users_path157 end158 it { should have_title('用户') }159 it { should have_content('所有用户') }160 describe "pagination" do161 before(:all) {30.times {FactoryBot.create(:user)}}162 after(:all) { User.delete_all}163 it { should have_selector('ul.pagination') }164 it "should list each user" do165 User.paginate(page: 1).each do |user|166 expect(page).to have_selector('li', text: user.name)167 end168 end169 end170 describe "delete links" do171 it { should_not have_link('delete') }172 describe "as an admin user" do173 let(:admin) { FactoryBot.create(:admin) }174 before do175 sign_in admin176 visit users_path177 end178 it { should have_link('delete', href: user_path(User.first)) }179 it "should be able to delete another user" do180 expect do181 click_link('delete', match: :first)182 end.to change(User, :count).by(-1)183 end184 it { should_not have_link('delete', href: user_path(admin)) }185 end186 describe "admin cannot delete himself (with request)" do187 let(:admin) {FactoryBot.create(:admin)}188 before { sign_in admin, no_capybara: true }189 describe "do delete method" do190 before { delete user_path(admin) }191 specify { expect(response).to redirect_to(root_path) }192 end193 end194 end195 end196 describe "following/followers" do197 let(:user) { FactoryBot.create(:user) }198 let(:other_user) { FactoryBot.create(:user) }199 before { user.follow!(other_user) }200 describe "followed users" do201 before do202 sign_in user203 visit following_user_path(user)204 end205 it { should have_title('Following') }206 it { should have_selector('h3', text: 'Following') }207 it { should have_link(other_user.name, href: user_path(other_user)) }208 end209 describe "followers" do210 before do211 sign_in other_user212 visit followers_user_path(other_user)213 end214 it { should have_title('Followers') }215 it { should have_selector('h3', text: 'Followers') }216 it { should have_link(user.name, href: user_path(user)) }217 end218 end219 describe 'viewing his lesson' do220 let(:user) { FactoryBot.create(:user) }221 let(:new_lesson) { FactoryBot.create(:course) }222 before { user.learn!(new_lesson) }223 describe 'learner' do224 before do225 sign_in user226 visit lessons_user_path(user)227 end228 it { should have_title('我的课程')}229 it { should have_selector('h3', text: '课程') }230 it { should have_link('详情', href: course_path(new_lesson)) }231 end232 end233 describe 'learning/unlearning a new lesson via click' do234 let(:user) { FactoryBot.create(:user) }235 let(:new_lesson) { FactoryBot.create(:course) }236 before { sign_in user }237 describe 'learning a lesson' do238 before { visit course_path(new_lesson) }239 it 'should increment the learning lessons count' do240 expect do241 click_button 'Add'242 end.to change(user.lessons, :count).by(1)243 end244 describe 'toggling the button' do245 before { click_button "Add" }246 it { should have_xpath("//input[@value='Remove']") }247 end248 end249 describe 'unlearning a lesson' do250 before do251 user.learn!(new_lesson)252 visit course_path(new_lesson)253 end254 it 'should decrement the learning lesson count' do255 expect do256 click_button 'Remove'257 end.to change(user.lessons, :count).by(-1)258 end259 describe 'toggling the button' do260 before { click_button 'Remove' }261 it { should have_xpath("//input[@value='Add']") }262 end263 end264 end265end...

Full Screen

Full Screen

job_posts_controller_spec.rb

Source:job_posts_controller_spec.rb Github

copy

Full Screen

2# Rspec API Documentation # https://rspec.info/documentation/4.0/rspec-rails/3RSpec.describe JobPostsController, type: :controller do4 describe "#new" do5 context "with user signed in" do6 before do7 session[:user_id] = FactoryBot.create(:user).id8 end9 it "renders the new template" do10 # GIVEN11 12 # WHEN13 get(:new)14 # The Rspec-Rails gem gives us this method to "mock" a request to the new action. More info at https://rspec.info/documentation/4.0/rspec-rails/RSpec/Rails/Matchers/RoutingMatchers/RouteHelpers.html#get-instance_method15 16 # THEN17 expect(response).to(render_template(:new))18 # response is an object that represents the HTTP-Response19 # Rspec makes this object available within the specs20 # We verify that the response will render out the template called "new" using the matcher `rendered_template`21 end22 23 it "sets an instance variable with a new job post" do24 # GIVEN25 26 # WHEN27 get(:new)28 29 # THEN30 # assign(:job_post) available from the `rails-controller-testing` gem allows you to grab an instance varaible31 expect(assigns(:job_post)).to(be_a_new(JobPost))32 # we check that the instance variable @job_post is a new instance of the class JobPost (Model)33 end34 end35 context "without signed in user" do36 it "should redirect to the sign in page" do37 get(:new)38 expect(response).to redirect_to(new_session_path)39 end40 end41 end42 describe "#create" do43 def valid_request44 post(:create, params: { job_post: FactoryBot.attributes_for(:job_post) })45 end46 context "with user signed in" do47 before do48 # mimics a user signed in49 session[:user_id] = FactoryBot.create(:user).id50 end51 context "with valid parameters" do52 53 it "creates a job post in the database" do54 # GIVEN55 count_before = JobPost.count # the number of all the records in the JobPost table56 # WHEN57 valid_request58 # mocking a post request to the create method. The params of the request will look similar to: {59 # job_post: {60 # title: 'senior dev',61 # description: 'lots of pay',62 # location: 'remote',63 # min_salary: 500_000,64 # max_salary: 1_000_00065 # }66 # }67 68 # THEN69 count_after = JobPost.count70 expect(count_after).to(eq(count_before + 1))71 # eq is an assertion provided by Rspec that checks the value to the right of the .to is equal to the paramater passed in to the method.72 end73 74 it "redirects us to the show page for that post" do75 # GIVEN76 # WHEN77 valid_request78 # THEN79 job_post = JobPost.last80 expect(response).to(redirect_to(job_post_url(job_post.id)))81 end82 end83 84 context "with invalid paramters" do85 def invalid_request86 post(:create, params: { job_post: FactoryBot.attributes_for(:job_post, title: nil) })87 # factorybot will create a invalid hash that looks like:88 # job_post: {89 # title: nil,90 # description: 'lots of pay',91 # location: 'remote',92 # min_salary: 500_000,93 # max_salary: 1_000_00094 # }95 # }96 end97 98 it "does not save a record in the database" do99 count_before = JobPost.count100 invalid_request101 count_after = JobPost.count102 expect(count_after).to eq(count_before)103 end104 105 it "renders the new template" do106 invalid_request107 expect(response).to render_template(:new)108 end109 110 end111 end112 context "with user not signed in" do113 it "should redirect to the sign in page" do114 valid_request115 expect(response).to redirect_to(new_session_path)116 end117 end118 119 end120 describe "#show" do121 it "render show template" do122 #GIVEN123 job_post = FactoryBot.create(:job_post)124 #WHEN125 get(:show, params: { id: job_post.id })126 #THEN127 expect(response).to render_template(:show)128 end129 it "sets an instance variable @job_post for the shown object" do130 job_post = FactoryBot.create(:job_post)131 get(:show, params: { id: job_post.id })132 expect(assigns(:job_post)).to eq(job_post)133 end134 end135 describe "#index" do136 it "render the index template" do137 get(:index)138 expect(response).to render_template(:index)139 end140 it "assign an instance variable @job_posts which contains all the created job posts" do141 job_post_1 = FactoryBot.create(:job_post)142 job_post_2 = FactoryBot.create(:job_post)143 job_post_3 = FactoryBot.create(:job_post)144 get(:index)145 expect(assigns(:job_posts)).to eq([job_post_3, job_post_2, job_post_1])146 end147 end148 describe "#destroy" do149 150 context "with signed in user" do151 152 context "as owner" do153 before do # will run all of this code before every single test within the this describe block154 current_user = FactoryBot.create(:user)155 session[:user_id] = current_user.id # sign in user156 #GIVEN157 @job_post = FactoryBot.create(:job_post, user: current_user)158 #WHEN159 delete(:destroy, params: { id: @job_post.id })160 end161 it "should remove a job post from the database" do162 #THEN163 expect(JobPost.find_by(id: @job_post.id)).to be(nil)164 end165 166 it "redirect to the job post index" do167 #THEN168 expect(response).to redirect_to(job_posts_path)169 end170 171 it "set a flash message" do172 expect(flash[:danger]).to be # assert that the danger property of the flash object exists173 end174 end175 context "as non owner" do176 before do177 current_user = FactoryBot.create(:user)178 session[:user_id] = current_user179 @job_post = FactoryBot.create(:job_post)180 # current logged in user IS NOT the user that owns @job_post181 end182 it "does not remove the job post" do183 delete(:destroy, params: { id: @job_post.id })184 expect(JobPost.find(@job_post.id)).to eq(@job_post)185 end186 end187 end188 end189 describe "#edit" do190 context "with signed in user" do191 context "as owner" do192 before do193 current_user = FactoryBot.create(:user)194 session[:user_id] = current_user.id # session[:user_id] is the same id as @job_post.user.id195 @job_post = FactoryBot.create(:job_post, user: current_user)196 end197 it "render the edit template" do198 get :edit, params: { id: @job_post.id }199 expect(response).to render_template :edit200 end 201 end202 context "as non owner" do203 before do204 current_user = FactoryBot.create(:user) 205 session[:user_id] = current_user.id # session[:user_id] is NOT the same as @job_post.user.id206 @job_post = FactoryBot.create(:job_post)207 end208 it "should redirect to show page" do209 get :edit, params: { id: @job_post.id }210 expect(response).to redirect_to job_post_path(@job_post)211 end212 end213 end214 end215 describe "#update" do216 before do217 @job_post = FactoryBot.create(:job_post)218 end219 context "with signed in user" do220 before do221 session[:user_id] = FactoryBot.create(:user)222 end223 context "with valid parameters" do224 225 it "update the job post record with new attributes" do226 new_title = "#{@job_post.title} Plus some changes!!!"227 patch :update, params: { id: @job_post.id, job_post: { title: new_title}}228 expect(@job_post.reload.title).to eq new_title229 end230 231 it "redirects to the show page" do232 new_title = "#{@job_post.title} Plus some changes!!!"233 patch :update, params: { id: @job_post.id, job_post: { title: new_title}}234 expect(response).to redirect_to(@job_post)...

Full Screen

Full Screen

songs_spec.rb

Source:songs_spec.rb Github

copy

Full Screen

1require 'rails_helper'2RSpec.describe SongsController, type: :request do3 describe "#show" do4 before do5 @song = FactoryBot.create(:song)6 end7 it "正常にレスポンスを返すこと" do8 get song_path(id: @song.id)9 expect(response).to be_successful10 end11 it "200レスポンスを返すこと" do12 get song_path(id: @song.id)13 expect(response).to have_http_status "200"14 end15 end16 describe "#create" do17 before do18 @user = FactoryBot.create(:user)19 end20 context "認可済みユーザーとして" do21 it "songレコードを登録できる" do22 login_as(@user)23 song_params = FactoryBot.attributes_for(:song)24 expect{25 post songs_path(song: song_params)26 }.to change(@user.songs, :count).by(1)27 end28 end29 context "ゲストとして" do30 it "302レスポンスを返すこと" do31 song_params = FactoryBot.attributes_for(:song)32 post songs_path(chord: song_params)33 expect(response).to have_http_status "302"34 end35 it "トップ画面にリダイレクトすること" do36 song_params = FactoryBot.attributes_for(:song)37 post songs_path(hord: song_params)38 expect(response).to redirect_to root_path39 end40 end41 end42 describe "update" do43 before do44 @user = FactoryBot.create(:user)45 @other_user = FactoryBot.create(:user)46 end47 context "オーナー権を持つユーザーとして" do48 before do49 @song = FactoryBot.create(:song, jam: false, user: @user)50 end51 it "songレコードを更新できる" do52 login_as(@user)53 song_params = FactoryBot.attributes_for(:song, jam: true)54 patch song_path(id: @song.id, song: song_params)55 expect(@song.reload.jam).to be_truthy56 end57 end58 context "オーナー権を持たないユーザーとして" do59 before do60 @song = FactoryBot.create(:song, jam: false, user: @other_user)61 end62 it "songレコードを更新できない" do63 login_as(@user)64 song_params = FactoryBot.attributes_for(:song, jam: true)65 patch song_path(id: @song.id, song: song_params)66 expect(@song.reload.jam).to be_falsey67 end68 end69 context "ゲストとして" do70 before do71 @song = FactoryBot.create(:song, jam: false, user: @other_user)72 end73 it "songレコードを更新できない" do74 song_params = FactoryBot.attributes_for(:song, jam: true)75 patch song_path(id: @song.id, song: song_params)76 expect(@song.reload.jam).to be_falsey77 end78 end79 end80 describe "destroy" do81 before do82 @user = FactoryBot.create(:user)83 @other_user = FactoryBot.create(:user)84 end85 context "オーナー権を持つユーザーとして" do86 before do87 @song = FactoryBot.create(:song, jam: false, user: @user)88 end89 it "songレコードを削除できる" do90 login_as(@user)91 expect{92 delete song_path(id: @song.id)93 }.to change(@user.songs, :count).by(-1)94 end95 end96 context "オーナー権を持たないユーザーとして" do97 before do98 @song = FactoryBot.create(:song, jam: false, user: @other_user)99 end100 it "songレコードを削除できない" do101 login_as(@user)102 expect{103 delete song_path(id: @song.id)104 }.to change(@user.songs, :count).by(0)105 end106 end107 context "ゲストとして" do108 before do109 @song = FactoryBot.create(:song, jam: false, user: @user)110 end111 it "songレコードを削除できない" do112 expect{113 delete song_path(id: @song.id)114 }.to change(@user.songs, :count).by(0)115 end116 end117 end118end

Full Screen

Full Screen

before

Using AI Code Generation

copy

Full Screen

1 name { Faker::Name.name }2 email { Faker::Internet.email }3 password { '12345678' }4 password_confirmation { '12345678' }5 before(:each) do6 @user = FactoryBot.create(:user)7 expect(@user).to be_valid8 expect(@user).to_not be_valid9 expect(@user).to_not be_valid10 expect(@user).to_not be_valid11 expect(@user).to_not be_valid12 expect(@user).to_not be_valid13 before(:each) do14 @user = FactoryBot.create(:user)15 expect(@user).to be_valid16 expect(@user).to_not be_valid17 expect(@user).to_not be_valid18 expect(@user).to_not be_valid19 expect(@user).to_not be_valid

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Factory_bot_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful