تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Starting spam tests0:00

Okay, let's write some tests for our spam management feature. So let's start with marking the Idea as spam, and I'm just going to duplicate one of our tests here. Let's duplicate our deleteIdea test. Let's go ahead and duplicate this, and say, spamManagementTest, okay? And let's change this as well. spamManagementTest. So let's start with this one. Let's change it to markAsSpam shows, markAsSpam Livewire component, or markIdeaAsSpam.

Let's change it to mark as spam shows, mark as spam Livewire component, or mark Idea as spam. So anyone who's logged in can do this, so we don't need to assign it to the same User. And let's just change this to mark Idea as spam. Okay, let's run that. CSS is next, the opposite case. So we just have to remove this acting as method, okay? And let's run that. Actually, let's change this. Mark Idea as spam, let's run that, okay?

Actually, let's change this. Mark Idea as spam, let's run that, okay? Next. Sorry, I forgot to change the name here. Let's not show mark Idea as spam. Next is that it actually works. So let's change the name again. Marking an Idea as spam works, okay? So again, we don't need to have the same User here. Let's test the mark Idea as spam class.

Testing mark-as-spam action1:43

So again, we don't need to have the same User here. Let's test the MarkIdeaAsSpam class. Let's make sure to import that. And we are calling the markIdeaAsSpam method. And we are asserting that an event is emitted, assertEmitted, or assertEmitted. And the event is, what was it? MarkIdeaAsSpam. Idea was marked as spam, okay? So let's run that. Let me comment this out for now.

So let's run that. Let me comment this out for now. Let's run that. Should pass. Doesn't. Sorry. The method name is markAsSpam. Let's try that again, okay? And now we just have to assert that the first Idea incremented the spamCount or the spamReports.

And now we just have to assert that the first Idea incremented the spam count or the spamReports. So let's say assertEquals one Idea first, and they should have a spamReports field. And it should be one. So let's try that. Okay. What's next? Next is we don't need the case for when it's an admin. We don't need this case as well as it's specific to ideas. Should I change the name of that?

We don't need this case as well as it's specific to ideas. Should I change the name of that? Marking an idea as spam, okay? Let's add one for this. So it does not work. So when we're not logged in, marking an idea as spam does not work. Okay. So let me just change this. Mark idea as spam and mark as spam. Let's make sure we get this status.

Next is marking an Idea as spam shows on the menu. Okay. So we don't even need this. We just have to be logged in. So we're passing in an Idea and we need the votesCount and let's see. It's called markAsSpam. So let's just say that markAsSpam, okay? And this should pass, does not, sorry, should be this. I think I have a lowercase a here. Okay.

I think I have a lowercase a here. Okay. And the last one, let's change the name. Marking an Idea as spam does not show. Okay. So this is if the User is not logged in. So we don't even need this. It's not logged in. It's test. And let's change this.

Adding mark-not-spam tests5:18

So I'm just going to go ahead and do it here and come back when it's done. Okay. So I added the cases for marking an Idea as not spam. And like I said, it's pretty much the same as mark as spam. The main difference is the User has to be an admin to be authorized. And for this case in particular, I'm just checking if the User is an admin and I set the spamReports to something that's not zero. I run the markNotAsSpam method, and then I assert that it's reset back to zero here. So if I go ahead and run this entire file, everything is passing. So now let's write a test for checking if the spamReports text shows.

Testing spamReports display5:52

So if I go ahead and run this entire file, everything is passing. So now let's write a test for checking if the spamReports text shows. So if you remember here, if I mark this as spam, we should see this spamReports. And this happens on this show page and also on the index page here. So let's do that. Let's just grab one of these cases here. I'll put underneath. Let's duplicate this. And let's change the name to spamReportsCount shows on ideas index page if logged in as admin.

And let's change the name to spamReports. Count shows on ideas index page if logged in as admin. Okay. So let's create a User. Let's grab it from here. Let's make sure that user is an admin and they are here. Okay. So let's create an Idea and let's set the spamReports. spamReports, say four. Actually, I'm going to change it to three here, and we are testing the idea index.

Spam reports, say four. Actually, I'm going to change it to three here, and we are testing the Idea index. And we actually need the actingAs method because we have to be logged in as an admin. Let's go ahead and test this component passing in the Idea and assert that we see spam reports and three. Okay. Let's test this out. Does not work. Make sure to import Idea index. Okay.

Make sure to import Idea index. Okay. Run it again. And it does pass now. And let's do the same thing for the one on the show page. So spam reports count shows on Idea show page. Let's change this to Idea show page and same with this one. Okay. And same setup, except we are testing the Idea show page. Okay.

Testing spam ideas filter7:59

And same setup, except we are testing the Idea show page. Okay. And it should be the same. Okay. So now the last test I'm going to write is the filter for spam Ideas. So since I already have a class dedicated for filters or for this case, other filters called OtherFiltersTest, I'm just going to put it in here. So I'm just going to make one test case here, and that's just to test if the filter actually works. So let's go ahead and write that test.

works. So let's go ahead and write that test. Let's call it spam. What I name it, spam, spam ideas, spam ideas, filter works. And let me just paste in some setup here. Okay. So I have a User that's an admin. I have four Ideas and three of them have a certain spam reports count. So this one should be first, second, third, and this one should default to zero. Okay.

So this one should be first, second, third, and this one should default to zero. Okay. So let's go ahead and make some assertions. So Livewire acting as the User. So we're logged in as the admin. Let's test the IdeasIndex class, which is where the filters are. Not sure if that's imported. So let's make sure it is okay. We're going to set the filter. So in this case, it's the filter variable, and I named it spamIdeas.

We're going to set the filter. So in this case, it's the filter variable, and I named it spamIdeas. Keep forgetting what I named it. spamIdeas. Right? Yeah. spamIdeas. So if you don't remember, we set the filter in here and that's a variable here. Okay. And we'll make use of the assertViewHas method to make sure everything is in order.

Okay. And we'll make use of the assertViewHas method to make sure everything is in order. So we're checking the ideas variable and it's passed through a callback function ideas. Okay. And let's make some assertions here. So return. Let's start with the count. So ideas count should be three because three of them have a spam report of at least one. So let's say three, let's see if this passes. And it does.

So let's say three, let's see if this passes. And it does. Cool. Let's continue making some assertions here. Let's just say the first one should be a certain Idea. And that Idea should be say ideas first title. So it should be the one with the highest spam report count. And that's this one. So Idea three, Idea three. And then the next one should be ideas get 1 title.

So Idea three, Idea three. And then the next one should be ideas get one title. So that should be Idea two. Okay. The last one should be Idea one. And that should be ideas get two. Okay. Did I do that right? Why is there squiggly? I need a ; here.

Running suite and committing11:20

Why is there squiggly? I need a semicolon here. Save that. Okay. Run that again. And it is passing. So let's go ahead and run our entire test suite with php artisan test for parallel. And everything is still passing. So as always, let's make a commit here. So git add, git commit, episode 44, manage spam test.

So as always, let's make a commit here. So git add, git commit, episode 44, manage spam test.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟