Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spec/factories/microposts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :micropost do
user
end
end
70 changes: 69 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,76 @@
end

describe 'validations' do
it { is_expected.to validate_presence_of(:name)}
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:password) }
end

describe "#validates" do
context 'when validates nil name' do
let(:user_nil_name) { User.new(name: '', password: 'test_password', email: 'test_email@test.com') }

it 'return false with nil name' do
result = user_nil_name.save
expect(result).to be false
end
end

context 'when validates nil password' do
let(:user_nil_password) { User.new(name: 'test_user_name', password: '', email: 'test_email@test.com') }

it 'return false with nil password' do
result = user_nil_password.save
expect(result).to be false
end
end

context 'when validates nil email' do
let(:user_nil_email) { User.new(name: 'test_user_name', password: 'test_password', email: '') }

it 'should return false with nil email' do
result = user_nil_email.save
expect(result).to be false
end
end

context 'validates exists email' do
let!(:user) { create(:user, email: "test_email@126.com") }
let(:new_user) { User.new(email: "test_email@126.com", password: SecureRandom.hex(4), name: SecureRandom.hex(3)) }

it 'return errors' do
result = new_user.save
expect(result).to be false
end
end
end

describe '#send_password_reset_email' do
let(:user) { User.new(name: "circle", email: "circle@feedmob.com", password: "123456") }

it "send password reset email" do
allow(UserMailer).to receive(:password_reset)
result = user.save
expect(result).to eq(true)
end
end

describe '#feed' do

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feed 方法测试需要加强,需要考虑 no feeds, 1 feed 和 many feeds 的情况

let!(:user) { create(:user) }

context "where no feed" do
it 'equal 0' do
expect(user.feed.size).to eq(0)
end
end

context "when 1 feed" do
let!(:user_1) { create(:user) }
let!(:micropost) { create(:micropost, user_id: user_1.id, content: "test1") }

it "equal 1" do
expect(user_1.feed.size).to eq(1)
end
end
end
end
65 changes: 64 additions & 1 deletion spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,67 @@
end
end
end
end

describe '#show' do
before do
@user = create(:user, activated: true, activated_at: Time.zone.now)
sign_in_as(@user)
end

subject { get user_path(@user) }

it 'should get user success' do
subject
expect(response).to have_http_status(200)
expect(@user.microposts.reload.size).to eq(0)
end
end

describe '#create' do
context "where create success" do

it 'should return success' do
post users_path, params: { user: {
name: "test",
password: "123123",
email: "test@email.com"
} }
expect(response).to have_http_status(302)
end
end

context "where create fail" do
it 'should return fail' do
post users_path, params: { user: {
name: "test",
password: "123123"
} }
expect(subject).to render_template(:new)
end
end
end


describe '#destroy' do
let(:user_delete) { create(:user, activated: true, activated_at: Time.zone.now) }

subject { delete user_path(user_delete) }

it 'should return success' do
sign_in_as(user_delete)
subject
expect(flash[:success]).to eq("User deleted")
end
end

describe '#following' do
let(:user) { create(:user, activated: true, activated_at: Time.zone.now) }
let(:user_following) { create(:user, activated: true, activated_at: Time.zone.now) }

it 'should be successful' do
sign_in_as(user)
get following_user_path(user)
expect(response).to render_template "show_follow"
end
end
end