Best Factory_bot_ruby code snippet using FactoryBot.reload
folder_spec.rb
Source:folder_spec.rb
...4 before :each do5 @user = FactoryBot.create :user6 @folder = FactoryBot.build :folder, user_id: @user.id7 @user.folders << @folder8 @folder.reload9 end10 context 'validations' do11 it 'always belongs to a user' do12 folder = FactoryBot.build :folder, user_id: nil13 expect(folder).not_to be_valid14 end15 it 'requires a title' do16 folder_nil = FactoryBot.build :folder, title: nil17 expect(folder_nil).not_to be_valid18 folder_empty = FactoryBot.build :folder, title: ''19 expect(folder_empty).not_to be_valid20 end21 it 'does not accept duplicate titles for the same user' do22 folder_dupe = FactoryBot.build :folder, user_id: @folder.user_id, title: @folder.title23 expect(folder_dupe).not_to be_valid24 end25 it 'accepts duplicate titles for different users' do26 folder_dupe = FactoryBot.build :folder, title: @folder.title27 expect(folder_dupe.user_id).not_to eq @folder.user_id28 expect(folder_dupe).to be_valid29 end30 end31 context 'association with feeds' do32 before :each do33 @feed1 = FactoryBot.create :feed34 @feed2 = FactoryBot.create :feed35 @feed3 = FactoryBot.create :feed36 @user.subscribe @feed1.fetch_url37 @user.subscribe @feed2.fetch_url38 @user.subscribe @feed3.fetch_url39 @folder.feeds << @feed1 << @feed240 end41 it 'returns feeds associated with this folder' do42 expect(@folder.feeds).to include @feed143 expect(@folder.feeds).to include @feed244 end45 it 'does not return feeds not associated with this folder' do46 expect(@folder.feeds).not_to include @feed347 end48 it 'does not allow associating the same feed more than once' do49 expect(@folder.feeds.count).to eq 250 expect(@folder.feeds.where(id: @feed1.id).count).to eq 151 @folder.feeds << @feed152 expect(@folder.feeds.count).to eq 253 expect(@folder.feeds.where(id: @feed1.id).count).to eq 154 end55 it 'allows associating a feed with at most one folder for a single user' do56 # user has two folders, each one with a feed. He's also subscribed to a feed that isn't in any folder57 user = FactoryBot.create :user58 feed = FactoryBot.create :feed59 feed2 = FactoryBot.create :feed60 feed3 = FactoryBot.create :feed61 folder1 = FactoryBot.build :folder, user_id: user.id62 folder2 = FactoryBot.build :folder, user_id: user.id63 user.folders << folder1 << folder264 user.subscribe feed.fetch_url65 user.subscribe feed2.fetch_url66 user.subscribe feed3.fetch_url67 folder1.feeds << feed268 folder2.feeds << feed369 folder1.feeds << feed70 expect(folder1.reload.feeds).to include feed71 expect(folder2.reload.feeds).not_to include feed72 folder2.feeds << feed73 expect(folder1.reload.feeds).not_to include feed74 expect(folder2.reload.feeds).to include feed75 end76 context 'add feed to folder' do77 it 'removes feed from any folders from the same user when associating with the new folder' do78 folder2 = FactoryBot.build :folder, user_id: @user.id79 @user.folders << folder280 feed2 = FactoryBot.create :feed81 @user.subscribe feed2.fetch_url82 @folder.feeds << feed283 expect(@folder.feeds).to include @feed184 folder2.feeds << @feed185 @folder.reload86 expect(@folder.feeds).not_to include @feed187 expect(folder2.feeds).to include @feed188 end89 it 'does not change feed association with folders for other users' do90 # folder2 is owned by @user91 folder2 = FactoryBot.build :folder, user_id: @user.id92 @user.folders << folder293 # @user has @feed1 in @folder; user2 has @feed1 in folder394 user2 = FactoryBot.create :user95 folder3 = FactoryBot.build :folder, user_id: user2.id96 user2.folders << folder397 user2.subscribe @feed1.fetch_url98 folder3.feeds << @feed199 expect(@folder.user_id).to eq @user.id100 expect(@folder.feeds).to include @feed1101 expect(folder2.user_id).to eq @user.id102 expect(folder2.feeds).not_to include @feed1103 expect(folder3.user_id).to eq user2.id104 expect(folder3.feeds).to include @feed1105 folder2.feeds << @feed1106 @folder.reload107 folder2.reload108 folder3.reload109 expect(@folder.user_id).to eq @user.id110 expect(@folder.feeds).not_to include @feed1111 expect(folder2.user_id).to eq @user.id112 expect(folder2.feeds).to include @feed1113 expect(folder3.user_id).to eq user2.id114 expect(folder3.feeds).to include @feed1115 end116 it 'deletes old folder only if it has no more feeds' do117 # @feed3 is in folder2 is owned by @user118 folder2 = FactoryBot.build :folder, user_id: @user.id119 @user.folders << folder2120 # Move @feed1 from @folder to folder2. @folder1 should not be deleted because @feed2 is still in it121 folder2.feeds << @feed1122 expect(Folder.where(id: @folder.id)).not_to be_blank123 # Move @feed1 from folder2 to @folder. folder should be deleted because it has no more feeds124 @folder.feeds << @feed1125 expect(Folder.where(id: folder2.id)).to be_blank126 end127 end128 context 'remove feed from folder' do129 it 'removes feed from folder' do130 @folder.feeds.delete @feed1131 @folder.reload132 expect(@folder.feeds).not_to include @feed1133 end134 it 'deletes folder if there are no more feeds in it' do135 @folder.feeds.delete @feed2136 expect(@folder.feeds.count).to eq 1137 expect(@folder.feeds).to include @feed1138 @folder.feeds.delete @feed1139 expect {Folder.find @folder.id}.to raise_error ActiveRecord::RecordNotFound140 end141 it 'does not change feed association with other folders' do142 folder2 = FactoryBot.create :folder143 folder2.user.subscribe @feed1.fetch_url144 folder2.feeds << @feed1145 @folder.feeds.delete @feed1146 @folder.reload147 expect(@folder.feeds).not_to include @feed1148 expect(folder2.feeds).to include @feed1149 end150 end151 end152 context 'association with entries' do153 it 'retrieves all entries for all feeds in a folder' do154 feed1 = FactoryBot.create :feed155 feed2 = FactoryBot.create :feed156 @user.subscribe feed1.fetch_url157 @user.subscribe feed2.fetch_url158 @folder.feeds << feed1 << feed2159 entry1 = FactoryBot.build :entry, feed_id: feed1.id160 entry2 = FactoryBot.build :entry, feed_id: feed1.id161 entry3 = FactoryBot.build :entry, feed_id: feed2.id162 feed1.entries << entry1 << entry2163 feed2.entries << entry3164 expect(@folder.entries.count).to eq 3165 expect(@folder.entries).to include entry1166 expect(@folder.entries).to include entry2167 expect(@folder.entries).to include entry3168 end169 end170 context 'sanitization' do171 it 'sanitizes title' do172 title_unsanitized = '<script>alert("pwned!");</script>folder_title'173 title_sanitized = 'folder_title'174 folder = FactoryBot.create :folder, title: title_unsanitized175 expect(folder.title).to eq title_sanitized176 end177 end178 context 'subscriptions_updated_at' do179 it 'defaults to the current date/time' do180 now = Time.zone.parse '2000-01-01'181 allow_any_instance_of(ActiveSupport::TimeZone).to receive(:now).and_return now182 folder = FactoryBot.build :folder, subscriptions_updated_at: nil183 @user.folders << folder184 expect(folder.reload.subscriptions_updated_at).to eq now185 end186 context 'touches subscriptions' do187 before :each do188 @feed = FactoryBot.create :feed189 @user.subscribe @feed.fetch_url190 @folder.feeds << @feed191 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at192 end193 it 'when unsubscribed from a feed in the folder' do194 # Move a second feed into the folder, so that it is not deleted when unsubscribing from @feed195 feed = FactoryBot.create :feed196 @user.subscribe feed.fetch_url197 @folder.feeds << feed198 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at199 @user.unsubscribe @feed200 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at201 end202 it 'when the title of a feed in the folder changes' do203 @feed.reload.update title: 'another title'204 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at205 end206 it 'when the URL of a feed in the folder changes' do207 @feed.reload.update url: 'http://another.url.com'208 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at209 end210 context 'unread entries count' do211 it 'when adding a new entry to a feed in the folder' do212 entry = FactoryBot.build :entry, feed_id: @feed.id213 @feed.entries << entry214 SubscriptionsManager.recalculate_unread_count @feed, @user215 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at216 end217 it 'when marking an entry of a feed in the folder as unread' do218 entry = FactoryBot.build :entry, feed_id: @feed.id219 @feed.entries << entry220 @user.change_entries_state entry, 'read'221 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at222 @user.change_entries_state entry, 'unread'223 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at224 end225 it 'when deleting an entry from a feed in the folder' do226 entry = FactoryBot.build :entry, feed_id: @feed.id227 @feed.entries << entry228 SubscriptionsManager.recalculate_unread_count @feed, @user229 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at230 entry.destroy231 SubscriptionsManager.recalculate_unread_count @feed, @user232 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at233 end234 it 'when marking an entry of a feed in the folder as read' do235 entry = FactoryBot.build :entry, feed_id: @feed.id236 @feed.entries << entry237 SubscriptionsManager.recalculate_unread_count @feed, @user238 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at239 @user.change_entries_state entry, 'read'240 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at241 end242 it 'when marking all entries as read' do243 entry = FactoryBot.build :entry, feed_id: @feed.id244 @feed.entries << entry245 SubscriptionsManager.recalculate_unread_count @feed, @user246 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at247 @user.change_entries_state entry, 'read', all_entries: true248 SubscriptionsManager.recalculate_unread_count @feed, @user249 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at250 end251 it 'when marking all entries in the folder as read' do252 entry = FactoryBot.build :entry, feed_id: @feed.id253 @feed.entries << entry254 SubscriptionsManager.recalculate_unread_count @feed, @user255 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at256 @user.change_entries_state entry, 'read', whole_folder: true257 SubscriptionsManager.recalculate_unread_count @feed, @user258 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at259 end260 it 'when marking all entries of a feed in the folder as read' do261 entry = FactoryBot.build :entry, feed_id: @feed.id262 @feed.entries << entry263 SubscriptionsManager.recalculate_unread_count @feed, @user264 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at265 @user.change_entries_state entry, 'read', whole_feed: true266 SubscriptionsManager.recalculate_unread_count @feed, @user267 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at268 end269 end270 context 'moving feeds' do271 it 'when feed is moved into the folder' do272 feed = FactoryBot.create :feed273 @user.subscribe feed.fetch_url274 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at275 @folder.feeds << feed276 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at277 end278 it 'when feed is moved out of the folder' do279 # Move a second feed into the folder, so that it is not deleted when unsubscribing from @feed280 feed = FactoryBot.create :feed281 @user.subscribe feed.fetch_url282 @folder.feeds << feed283 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at284 @user.move_feed_to_folder @feed, folder: Folder::NO_FOLDER285 expect(@folder.reload.subscriptions_updated_at).not_to eq @old_subscriptions_updated_at286 end287 end288 end289 context 'does not touch subscriptions' do290 before :each do291 @feed = FactoryBot.create :feed292 @user.subscribe @feed.fetch_url293 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at294 end295 it 'when unsubscribed from a feed not in the folder' do296 @user.unsubscribe @feed297 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at298 end299 it 'when the title of a feed not in the folder changes' do300 @feed.reload.update title: 'another title'301 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at302 end303 it 'when the URL of a feed not in the folder changes' do304 @feed.reload.update url: 'http://another.url.com'305 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at306 end307 context 'unread entries count' do308 it 'when adding a new entry to a feed not in the folder' do309 entry = FactoryBot.build :entry, feed_id: @feed.id310 @feed.entries << entry311 SubscriptionsManager.recalculate_unread_count @feed, @user312 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at313 end314 it 'when marking an entry of a feed not in the folder as unread' do315 entry = FactoryBot.build :entry, feed_id: @feed.id316 @feed.entries << entry317 @user.change_entries_state entry, 'read'318 @subscriptions_updated_at = @folder.reload.subscriptions_updated_at319 @user.change_entries_state entry, 'unread'320 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at321 end322 it 'when deleting an entry from a feed not in the folder' do323 entry = FactoryBot.build :entry, feed_id: @feed.id324 @feed.entries << entry325 SubscriptionsManager.recalculate_unread_count @feed, @user326 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at327 entry.destroy328 SubscriptionsManager.recalculate_unread_count @feed, @user329 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at330 end331 it 'when marking an entry of a feed not in the folder as read' do332 entry = FactoryBot.build :entry, feed_id: @feed.id333 @feed.entries << entry334 SubscriptionsManager.recalculate_unread_count @feed, @user335 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at336 @user.change_entries_state entry, 'read'337 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at338 end339 it 'when marking all entries of a feed not in the folder as read' do340 entry = FactoryBot.build :entry, feed_id: @feed.id341 @feed.entries << entry342 SubscriptionsManager.recalculate_unread_count @feed, @user343 @old_subscriptions_updated_at = @folder.reload.subscriptions_updated_at344 @user.change_entries_state entry, 'read', whole_feed: true345 SubscriptionsManager.recalculate_unread_count @feed, @user346 expect(@folder.reload.subscriptions_updated_at).to eq @old_subscriptions_updated_at347 end348 end349 end350 end351end...
process_spec.rb
Source:process_spec.rb
...134 # and is therefore processed first - gets approved.135 # waiver_picks 6 and 7 are both declined since in_player_2 is taken136 # waiver_picks 9-10 both have a pick_number of 3 and are trying to trade in in_player_3137 # waiver_pick_10 is processed first since fpl_team_list_3 has a lower round rank (3) - gets approved138 expect(WaiverPick.approved).to contain_exactly(waiver_pick_5.reload, waiver_pick_8.reload, waiver_pick_10.reload)139 expect(WaiverPick.declined).to contain_exactly(140 waiver_pick_1.reload,141 waiver_pick_2.reload,142 waiver_pick_3.reload,143 waiver_pick_4.reload,144 waiver_pick_6.reload,145 waiver_pick_7.reload,146 waiver_pick_9.reload,147 )148 expect(fpl_team_5.players.reload).to contain_exactly(in_player_1)149 expect(fpl_team_list_5.players.reload).to contain_exactly(in_player_1)150 expect(list_position_5.reload.player).to eq(in_player_1)151 expect(fpl_team_4.players.reload).to contain_exactly(in_player_2)152 expect(fpl_team_list_4.players.reload).to contain_exactly(in_player_2)153 expect(list_position_4.reload.player).to eq(in_player_2)154 expect(fpl_team_3.players.reload).to contain_exactly(in_player_3)155 expect(fpl_team_list_3.players.reload).to contain_exactly(in_player_3)156 expect(list_position_3.reload.player).to eq(in_player_3)157 expect(league.reload.players).to contain_exactly(in_player_1, in_player_2, in_player_3, out_player_1, out_player_2)158 end159 it 'does not process waiver picks prior to the waiver_pick cut_off time' do160 round = FactoryBot.create(:round, is_current: true, deadline_time: 1.day.from_now + 1.minute)161 league = FactoryBot.create(:league, status: 'active')162 fpl_team = FactoryBot.create(:fpl_team, league: league, rank: 1)163 fpl_team_list = FactoryBot.create(:fpl_team_list, fpl_team: fpl_team, round: round)164 list_position = FactoryBot.create(:list_position, :fwd, fpl_team_list: fpl_team_list)165 fpl_team.players << list_position.player166 league.players << list_position.player167 FactoryBot.create(168 :waiver_pick,169 round: round,170 league: league,171 fpl_team_list: fpl_team_list,...
project_member_spec.rb
Source:project_member_spec.rb
...111 :active].sample,112 inviter_id: FactoryBot.create(:user).id)113 project_member.send_confirmation_email114 project_member.save115 project_member.reload116 expect(project_member.confirmation_sent_at).to be_present117 end118 it "invite true" do119 user = FactoryBot.create(:user)120 project_member =121 FactoryBot.create(:project_member_details)122 project_member.update(invite: true,123 new_inviter_id: user.id)124 project_member.reload125 expect(project_member.confirmation_sent_at).to be_present126 expect(project_member.inviter_id).to eq(user.id)127 end128 context "validates_acceptance of dms and cms modules" do129 it 'if creator and project admin' do130 member = FactoryBot.create(:project).reload.members.first131 member.cms_module_access = false132 member.save133 member.reload134 expect(member.cms_module_access).to be_truthy135 end136 it 'if regular member' do137 member = FactoryBot.create(:project_member,138 cms_module_access: true)139 member.reload140 member.cms_module_access = false141 member.save142 member.reload143 expect(member.cms_module_access).to be_falsy144 end145 it "if invited admin" do146 admin = FactoryBot.create(:project_admin)147 admin.reload148 admin.cms_module_access = false149 admin.save150 admin.reload151 expect(admin.cms_module_access).to be_truthy152 end153 end154 context "admin?" do155 it "should be true" do156 project_admin = FactoryBot.create(:project_admin)157 expect(project_admin.admin?).to be_truthy158 end159 it "should be false" do160 project_member = FactoryBot.create(:project_member)161 expect(project_member.admin?).to be_falsey162 end163 end164 context "validate last_admin" do165 let(:project) { FactoryBot.create(:project) }166 let(:project_member) { project.reload.admins.first }167 it "shouldn't be editable with only one admin" do168 project_member.role = 169 project.roles.where.not(title: "Project Administrator").sample170 project_member.save171 expect(project_member.reload.role.title).to eq("Project Administrator")172 end173 it "should be editable if the project has two admins" do174 FactoryBot.create(:project_member,175 project_id: project.id,176 role: project.roles.find_by(title: "Project Administrator"))177 project_member.role = 178 project.roles.where.not(title: "Project Administrator").sample179 project_member.save180 expect(project_member.reload.role.title).not_to eq("Project Administrator")181 end182 end183end...
reload
Using AI Code Generation
1user = FactoryBot.create(:user)2user = FactoryBot.create(:user)3user = FactoryBot.create(:user)4user = FactoryBot.create(:user)5user = FactoryBot.create(:user)
reload
Using AI Code Generation
1user = FactoryBot.create(:user)2user = FactoryBot.create(:user)3user = FactoryBot.create(:user)4user = FactoryBot.create(:user)5user = FactoryBot.create(:user)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!