惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
S
SegmentFault 最新的问题
Project Zero
Project Zero
D
DataBreaches.Net
I
InfoQ
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Vercel News
Vercel News
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
Intezer
The Hacker News
The Hacker News
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
T
Threatpost
爱范儿
爱范儿
N
Netflix TechBlog - Medium
D
Docker
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
K
Kaspersky official blog
H
Help Net Security
S
Secure Thoughts
T
Threat Research - Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
G
Google Developers Blog
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
B
Blog
Google DeepMind News
Google DeepMind News
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
P
Privacy International News Feed
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog

Rc-2020 on Julia Evans

Day 57: Trying to set up GitHub Actions Day 56: A little WebAssembly Day 53: a little nginx, IPv6, and wireguard Day 52: testing how many Firecracker VMs I can run Day 51: Fixed my logging and made a couple of puzzles Day 50: Building some tarballs for puzzles, and trying to make a kernel boot faster Day 49: making the VMs boot faster Day 48: Another Go program, and a little vim configuration Day 47: Using device mapper to manage Firecracker images Day 46: debugging an iptables problem Day 44: Building my VMs with Docker Day 43: Building VM images Day 42: Writing a Go program to manage Firecracker VMs Day 41: Trying to understand what a bridge is Day 40: screen flickering & a talk about containers Day 39: Customizing gotty's terminal Day 38: Modifying gotty to serve many different terminal applications at once Day 37: A new laptop and a little Vue Day 35: Launching my VMs more reliably Day 34: Learning about qemu Day 33: pairing is magic and beautiful git diffs Day 32: A Rails model that doesn't use the database with ActiveHash Day 24: a short talk about blogging myths, and a debugging tip Day 22: getting OAuth to work in Rails Day 21: wrangling systemd & setting up git deploys to a VM Day 19: Clustering faces (poorly) using an autoencoder Day 20: trying to figure out how Google Cloud IAM works Day 18: an answer to an autoencoder question Day 17: trying to wrap my head around autoencoders Day 13: BPTT, and debugging why a model isn't training is hard Day 11: learning about learning rates Day 10: Training an RNN to count to three Day 9: Generating a lot of nonsense with an RNN Day 8: Start with something that works Day 5: drawing lots of faces with sketch-rnn Day 3: an infinitely tall fridge Day 2: Rails associations & dragging divs around Day 1: a confusing Rails error message I'm doing another Recurse Center batch!
Day 23: a little Rails testing
Julia Evans · 2020-12-11 · via Rc-2020 on Julia Evans

I’ve been working on a little Rails app to manage a bunch of virtual machines that run programming puzzles, and in the last couple days I ran into a problem I run into a lot: I forgot about testing!

here’s how it goes:

  1. start writing some code with no tests, think “this is fine”
  2. 2 days later, realize I’m spending a lot of time manually testing and fixing the same bug 3 times because I broke it again
  3. remember that automated testing exists
  4. write tests and everything is 10x better

A few small nice things that I’ve found in my first day or two of doing testing in Rails:

my Rails app comes with tests!

I’ve haven’t really used Rails much before, and when I started writing tests I noticed that rails generate scaffold Puzzle had generated a bunch of tests for me for the Puzzle controller!

This was really nice because it gave me a template to start from

there are fixtures!

Every model has a corresponding file called test/fixtures/MODEL_NAME.yml that has a bunch of data used to create test objects. For example, here are some fixtures for my VirtualMachineInstance model:

manuela: 
   email: manuela@example.com
rishi:
   email: rishi@example.com

This means that I can quickly create a test object like this:

@user = users(:rishi)

there are authentication helpers to help manage logins!

I’m using a gem called Devise right now to handle logins, and it comes with a bunch of test helpers to pretend that I’m logged in to the site when accessing a page. This is super useful in integration tests!

Here’s what that looks like

  setup do
    @user = users(:rishi)
    @user.save
    login_as(@user, :scope => :user)
  end

mocking HTTP requests is easy

I have some code in my project that talks to an API to launch virtual machine instances. I obviously don’t actually want to launch VM instances in my tests (could get expensive!), so I wanted to mock out the calls to the API. Here’s how that works:

WebMock.disable_net_connect!
stub_request(:get, "https://api.digitalocean.com/v2/account/keys?page=1&per_page=20").
  to_return(status: 200, body: '{"ssh_keys":[],"links":{},"meta":{"total":2}}')

The nice thing is that WebMock.disable_net_connect! prevents Ruby from making any external API requests, and if one happens then it prints out an example of some code I could write to mock that request.

integration tests seem easy at first

I was spending a lot of time clicking on links and making sure that if it worked if I went to X page and then Y page from there.

I was surprised by how easy Rails makes it to write integration tests! Here’s an example of an integration test that I have that makes sure that right after you start a puzzle (which launches an instance), the instance’s status is “pending”. So little code!

test "status is pending right after instance started" do
    get '/puzzles/1/start'
    get '/instances/220816290/status'
    assert_response :success
    assert_equal({"status" => "pending"}, response.parsed_body)
end

This code definitely has a “magical Ruby” feel but I don’t really mind and it’s fun to write so far.

that’s all!

One thing I really appreciate about Rails is that there are like 15 million blog posts and Stack Overflow answers about using it, which has made it pretty easy so far to get all my questions answered.

It really feels like if I’m confused about something, many thousands of people have been confused about the exact same thing and have written about it in 1000 different places, which is a nice change of pace from some of the weirder things I’ve tried to learn about.